Search code examples
androidbashshellstdinbusybox

Android shell using busybox commands as default


I'm running a script on android and in effort to make it as portable as possible, all commands use busybox. How I've got it set up currently, is every command has a function named the same, so it converts those commands to use busybox, like so:

echo () {
busybox echo $1 $2 $3 $4 $5
}
echo "hai"

As this has to be done for every command, it takes a lot of space inside the script. That's why I'm trying to figure out a way to force the shell to default to using busybox rather than /system/bin or /system/xbin. Could this be achieved by modifying the PATH variable? Or is there an environment variable build into shell I could use?

Or should I do something like this?:

bs () {
busybox $@
}

bs echo "Some text"

(I'd like to avoid this if possible as it decreases readability)

EDIT

Could I start a background process that loops and when it detects a command being passed for the shell to processes, it stops this and passes it to busybox? Somehow read from stdin before shell processes it?

EDIT 2

So I thought about redirecting commands into busybox like this:

busybox <<EOF
echo "hai";
EOF

Could this be used somehow?

UPDATE

I've moved to using busyboxes ash shell and it does everything I want it to do. Appareantly there's no way of intercepting commands before they're passed to the shell.


Solution

  • Use the shell's built-in 'alias' (Alias Substitution, Shell Command Language, IEEE Standard 1003.1 (2013) ) method. For example:

    alias vi="busybox vi"
    vi readme.txt
    

    will substitute the word 'vi' with 'busybox vi' and the shell will then process

    busybox vi readme.txt

    Because it is a simple substitution, all parameters are preserved and passed on to the substituted command.

    You can also add default parameters to the substitution like so:

    alias fbset="busybox fbset -fb /dev/graphics/fb0"
    

    after which one no longer needs to specify the frame buffer - it is added for you. And typically programs will take the last specified parameter as the active one, so if you do need to specify another frame buffer (in the above example) it will ignore the one provided in the alias.