Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing scripts with exit or return code #28

Closed
bluecatpixel opened this issue Nov 23, 2018 · 1 comment
Closed

Executing scripts with exit or return code #28

bluecatpixel opened this issue Nov 23, 2018 · 1 comment

Comments

@bluecatpixel
Copy link

Hi @topjohnwu.

Great job and thanks for your work!

I tried the lib but while executing line commands everything is working fine when I try to execute scripts I found that the script never executes.

I was able to find that the problem is in the exit. Whenever I use exit (also in commands) the lib always returns code -1. I tried to substitute the exit by return but I always get the code -1.

I tried a sync call:

Shell.Result result = Shell.su(getResources().openRawResource(resId)).exec();
mTvCode.setText("Code: " + result.getCode());
List out = result.getOut();
StringBuilder sbOut = new StringBuilder();
if (out != null) {
    for (String o : out) {
        sbOut.append(o);
    }
}
mTvOutput.setText("Out: " + sbOut.toString());
List err = result.getErr();
StringBuilder sbErr = new StringBuilder();
if (err != null) {
    for (String e : err) {
        sbErr.append(e);
    }
}
mTvErr.setText("Err: " + sbErr.toString());

And I tried async call:

Shell.su(getResources().openRawResource(R.raw.script))
    .to(consoleList)
    .submit(result -> {
        mTvCode.setText("Code: " + result.getCode());
        List out = result.getOut();
        StringBuilder sbOut = new StringBuilder();
        if (out != null) {
            for (String o : out) {
                sbOut.append(o);
            }
        }
        mTvOutput.setText("Out: " + sbOut.toString());
        List err = result.getErr();
        StringBuilder sbErr = new StringBuilder();
        if (err != null) {
            for (String e : err) {
                sbErr.append(e);
            }
        }
        mTvErr.setText("Err: " + sbErr.toString());
    });

The result is always the same.

Here's one script example:

#!/system/bin/sh
function exit_error(){
    case "$1" in
        1)
            echo 'busybox is not installed'
            ;;
    esac
    #exit 2
    return 2
}

busybox > /dev/null || exit_error 1

if [[ ! -f /sdcard/file.txt ]]; then
    echo 'File not exists'
    #exit 1
    return 1
fi

echo 'Exiting'
#exit 0
return 0`

Am I doing something wrong or is this a limitation?

Thanks in advance.
JS

@topjohnwu
Copy link
Owner

You should never terminate the shell! The shell in libsu is a long living session, exiting will terminate the process, and thus it cannot retrieve the return code.
You can run that code in a subshell, e.g. (shell_script) or sh shell_script, which will fork another process from the shell, and exiting will only terminate the subshell but not the shell used in libsu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants