0

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}
Sandeep
  • 18,356
  • 16
  • 68
  • 108

2 Answers2

2

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks Barmar. I shall try it in some while and will get back to you. Bdw is there a way that we can find the shell type of Android shell? – Sandeep Nov 17 '16 at 04:58
  • Not sure. From what I've been able to find, it looks like it's something specific to ADB. – Barmar Nov 17 '16 at 05:05
  • It seems it is mksh shell. Alex has just now added a link in the comment section of question. I will check more.. – Sandeep Nov 17 '16 at 05:09
  • Hi Barmar, my troubles continue.. printf is not working as expected on mksh.. I posted a question - http://stackoverflow.com/questions/40668399/using-prinf-in-android-shell-mksh Please check if possible – Sandeep Nov 18 '16 at 02:25
  • I don't know anything about `mksh`, I'm not sure how I can help you with it. – Barmar Nov 18 '16 at 16:40
2

The adb shell was a direct descendant of BSD ash, of which dash is also. The adb shell in more recent android versions is mksh standard shell of non-emulator builds on Android 4.0 and newer.

From the error text it is probable that the shell is some mksh version.

As the mksh in android is under heavy changes and improvements, I recommend you to take dash as the target shell. It is available in most linux distributions and is the default system shell for debian and derivatives. It has less features but it is very difficult that something that work in dash will not work in the mksh of adb.

{1:3:1}

In particular, this expression:

x=${1:3:1}

has no meaning in dash (it does work, however, in mksh). It could be replaced with this:

x=${1#???}; x=${x%"${x#?}"}

A bit longer, but gets the job done in any shell.

[

Also, there is a problem with the […] test, it seems to have too many "parts" to be reliable. Dividing the test into two (or more if needed) is the usual remedy:

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

The || is also an or (with the same preference as &&, the and).
Which also works in any shell.

With a final change of (a missing $ for variable x):

 echo "A - ${a} X - ${x}"

We get the whole script as:

func()
{
    local x
    x=${1#???}; x=${x%${x#?}}
    echo "x - $x"
    if [ "${x}" = " " ] || [ -z "${x}" ]; then
      a="M"
    else
      a="S"
    fi
    echo "A - ${a} X - ${x}"
}

func "abc=efg"

And running it in dash, bash or mksh, gives:

x - =
A - S X - =
Community
  • 1
  • 1