shell script in android gives [: not found

2019-02-25 08:09发布

I have this script which works on my linux machine

#!/bin/sh
c=1
if [ $c == 1 ]
then
  echo c is 1
else
  echo c is 0
fi

But when I use this in android as follows:

#!/system/bin/sh
c=1
if [ $c == 1 ]
then
  echo c is 1
else
  echo c is 0
fi

It gives an error like:

[: not found

EDIT

Is there any other logic to check the value of $c, whether it is 1 or 0 ?

Android shell have problem with [] in if so is there any other way to check the value of c ?

8条回答
聊天终结者
2楼-- · 2019-02-25 08:13

Use bash:

#!/system/bin/bash

or

#!/system/xbin/bash

You can check where your sh binary is pointing to on your Linux machine:

ls -l /bin/sh

Edit

BTW, use:

c=1
if [ $c -eq 1 ]
then
  echo c is 1
else
  echo c is 0
fi
查看更多
疯言疯语
3楼-- · 2019-02-25 08:20

use /system/bin/cmp for equality test. if you need numerically test, substitute $(($c == 1)) with $c

#!/system/bin/sh
echo $c >/tmp/a
echo 1 >/tmp/b
if cmp /tmp/a /tmp/b
    echo c is 1
else
    echo c is 0
fi
查看更多
够拽才男人
4楼-- · 2019-02-25 08:21

I run into this issue also and found a solution (on another site)

if [[ $b -gt 0]]
then
    echo 'Hooray it works'
else
    echo 'still works'
fi
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-02-25 08:26

generally [ is an alias for test,

in Linux machine test is at

/usr/bin/test

and

if [ $c == 1 ]

is evaluated as

if test "$c" = 1

BUT here in android there is no test

so if with [] will not work in any case...

i will cross compile test for android and check it....!!!

查看更多
唯我独甜
6楼-- · 2019-02-25 08:27

Android does not provide a full UNIX environment, it is not a UNIX operating system. It has some similarities, much like how Windows also has some similarities to UNIX. Some Android devices and ROMs try to provide more of a UNIX-like environment that others, but you cannot rely on most of the standard shell scripting tools being installed if you are thinking about cross-device compatibility.

So for example, if you look at your GNU/Linux system, you can see that test and [ are actually programs. Try this: ls -l /usr/bin/[. Most Android installs do not include test or [. That means that if you want to try to do actual programming with Android's minimal shell environment, you have to use lots of odd tricks. You can install busybox to get a full UNIX shell environment, or you can even build busybox into your app. I do that when I need to include shell scripts in an app (for example, Lil' Debi and Commotion MeshTether).

Here's an example of writing a killall in Android's /system/bin/sh environment: http://en.androidwiki.com/wiki/Android_Shell_tips_and_tricks You can also use the various parameter expansions to create some logic, you can see an example of that in the Barnacle Wifi Tether scripts.

查看更多
劳资没心,怎么记你
7楼-- · 2019-02-25 08:30

Think you using the wrong arithmetic operator and there is a syntax error of a missing ";": try

[ $c -eq 1 ];

Also your location for Bash (sh) might be wrong at the top of your file:

#!/system/bin/sh
查看更多
登录 后发表回答