I'm trying to run a script from my Android app, the .sh is in /res/raw folder which eventually is copied to internal storage, and rooting the device is not an option.

But I'm facing this error: Error running exec(). Command: [.....] where the cause is java.io.IOException: Permission denied; I've been trying to run the bash for 2 days, but at this point I do not see further, hope someone with fresher mind can see what's missing.

I already tried to run chmod 775 as well

Script (test.sh):

#!/bin/bash
REQUESTDATE=`date "+%Y-%m-%dT%H:%M:%S"`
URL=https://.....
Resp=`curl -v -H "date: $REQUESTDATE" -H "Content-Type: application/json" -X POST -d@my_json.json $URL`
echo $Resp

On my Android app:

private void runCommand(){
        String pathToScript = getDir("my_scripts",Context.MODE_PRIVATE).getAbsolutePath() + File.separator + "test.sh";
        String[] commands = {"bash", "-c", pathToScript};

        try{
            java.lang.Process process = new ProcessBuilder()
                    .command(commands)
                    .directory(new File (getDir("my_scripts",Context.MODE_PRIVATE).getAbsolutePath()))
                    .redirectErrorStream(true)
                    .start();

            //Read output
            StringBuilder output = new StringBuilder();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = bufferedReader.readLine()) != null){
                output.append(line + "\n");
            }

            if (process.waitFor() == 0){
                Log.i(TAG, "Success");
            } else{
                e.printStackTrace();
            } 
        }catch(IOException e){
        e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
}

Related posts

Recent Viewed