Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we run simple shell script in android system using react native?

I am writing a simple app using react native to make some file operations done automatically. I have tried packages like strong text react-native-fs, but I found that there are some files/folders that can not be read using

RNFS.ls(PATH). 

Trying to list files in that folder will throw an exception.

However, these files can be displayed using ls command in adb shell. So I am wondering if there is a way that we can run shell commands in react native like we make some system calls in java/python?

Thanks

like image 303
linjunshi Avatar asked Oct 28 '22 23:10

linjunshi


1 Answers

Write your own Native Module.

You can easily run a shell command from Java and call it from React Native using React Native's Native Modules. You can find more information here:

https://facebook.github.io/react-native/docs/0.60/native-modules-android#callbacks

And for your Java command, you'd want to use something like this:

String command = "ls " + path; // Where path is your desired path.

Runtime runtime = Runtime.getRuntime();

Process result = runtime.exec( command );

BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( result.getInputStream() ) );

String[] parts = bufferedReader.readLine().split( "\\s+" );
like image 79
Joshua Pinter Avatar answered Nov 09 '22 06:11

Joshua Pinter