shell script in android gives [: not found

旧城冷巷雨未停 提交于 2019-12-01 21:15:38

andriod shell sh is actually a link to busybox, and it is invoked as

busybox sh

you need setup [ applets manually

busybox ln -s /your_original_sh_path/busybox [ 

if you don't know where busybox is put, try list the /system/bin/sh which you give

ls /system/bin/sh
busybox which busybox

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....!!!

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.

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

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

How about checking that the .sh file doesn't contain a carriage return before line feed.

Windows \r\n -> CR LF

Unix \n -> LF

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!