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 ?
Use bash:
#!/system/bin/bash
or
#!/system/xbin/bash
You can check where your
sh
binary is pointing to on your Linux machine:Edit
BTW, use:
use /system/bin/cmp for equality test. if you need numerically test, substitute $(($c == 1)) with $c
I run into this issue also and found a solution (on another site)
generally
[
is an alias fortest
,in Linux machine test is at
and
is evaluated as
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 includetest
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.Think you using the wrong arithmetic operator and there is a syntax error of a missing ";": try
Also your location for Bash (sh) might be wrong at the top of your file: