Shell script works in bash but not on ‘adb shell’

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error Shell script works in bash but not on ‘adb shell’ on this date .

Below shell script works perfectly fine in bash shell. But produces an error in android shell. May be it has got to do something with the shell that android is using. But how can I resolve it? If not a perfect solution, an hack would also do for me.

Shell-script :

func()
{
    local x
    x=${1:3:1}
    echo "x - $x"
    if [ "${x}" = " " -o -z "${x}" ]; then
      a="M"
    else
      a="S"
    fi
    echo "A - ${a} X - {x}"
}

func "abc=efg"

O/p In android shell,

root:/ # sample.sh                                                  
x - =
/system/bin/sample.sh[14]: [: =: missing second argument
A - S X - {x}

O/p on bash shell(Linux):

PC:~$ ./sample.sh 
x - =
A - S X - {x}

Answer

When the value of $x is just =, it looks like:

if [ = = " " -o -z = ]; then

and it’s apparently getting confused by the = that should be a value rather than an operator.

Change it to:

if [ "x${x}" = "x " -o "x${x}" = "x" ]; then