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} 
9
  • Does the script begin with #!/bin/bash?
    – Barmar
    CommentedNov 17, 2016 at 4:43
  • @Barmar yes. It begins with #!/bin/bash on bash shell and #!/system/bin/sh on Android shell
    – Sandeep
    CommentedNov 17, 2016 at 4:44
  • What's the version of bash on Android?
    – Barmar
    CommentedNov 17, 2016 at 4:46
  • @Barmar I think Android does not use bash. There is no executable /system/bin/bash
    – Sandeep
    CommentedNov 17, 2016 at 4:46
  • 1
    @Barmar the line "= = =" is creating the problem
    – Sandeep
    CommentedNov 17, 2016 at 4:49

2 Answers 2

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 
5
  • 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
    CommentedNov 17, 2016 at 4:58
  • Not sure. From what I've been able to find, it looks like it's something specific to ADB.
    – Barmar
    CommentedNov 17, 2016 at 5: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
    CommentedNov 17, 2016 at 5:09
  • Hi Barmar, my troubles continue.. printf is not working as expected on mksh.. I posted a question - stackoverflow.com/questions/40668399/… Please check if possible
    – Sandeep
    CommentedNov 18, 2016 at 2:25
  • I don't know anything about mksh, I'm not sure how I can help you with it.
    – Barmar
    CommentedNov 18, 2016 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 mkshstandard 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 - = 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.