1

I keep getting syntax errors with this somewhat basic script whenever running it on android by calling bash ping.sh. Currently the error is : command not found ping.sh: line 9: syntax error near unexpected token etc. Here's my script:

    #!/system/bin/sh

# check if the first argument is -all, in which case just ping all
# possible hosts
if [ $# -ge 1 ]; then
    if [ $1 == "-all" ]
    then
        # loop through all IPs
        for ((host=1; host<100; host++))
        do
            ping -c3 192.168.0.$host > /dev/null && echo "192.168.0.$host UP"
        done
    else
        # loop through the hosts passed in
        while test $# -gt 0 # while number of arguments is greater than 0
        do
            ping -c3 $1 > /dev/null && echo "$1 UP" || echo "$1 DOWN"
            shift # shift to the next argument, decrement $# by 1
        done
    fi
else
# if the number of arguments is 0, return a message stating invalid input
    echo "No arguments specified. Expected -all or host names/ip addresses."
    echo "Usage: ping: -all"
    echo "Or: ping: 192.168.0.1,192.168.0.16"
fi
linucksrox
  • 150
  • 1
  • 13
  • does the device is even have the ping executable. use `adb shell` to find out – petey Apr 19 '13 at 16:17
  • Your device doesn't have ping. Only a small subset of what you'd normally expect to find in a linux install is actually on the device, and it differs by model. You can't count on much more than ls and rm. – Gabe Sechan Apr 19 '13 at 16:19
  • you are better off trying to do this via java code check out this question and its answers: http://stackoverflow.com/questions/11506321/java-code-to-ping-an-ip-address – petey Apr 19 '13 at 16:31
  • Android devices do not normally have bash - they have a very limited shell from the toolbox package which was created for Android. Some custom roms have a busybox shell as well or instead. Porting actual bash is possible and has likely been done, but would be a bit unusual. – Chris Stratton Apr 19 '13 at 18:49

2 Answers2

3

android shell is not GNU bash shell, but a POSIX shell (NetBSD Almquist shell prior to 2.x, MirBSD Korn Shell from 3.0 onwards).

[ $1 == "-all" ] is Bashism, for ((host=1; host<100; host++)) is another Bashism.

for making it work in POSIX shell, rewriting some lines is needed:

#!/system/bin/sh

# check if the first argument is -all, in which case just ping all
# possible hosts
if [ $# -ge 1 ]; then
    if [ $1 = "-all" ]
    then
        # loop through all IPs
        host=1; while test $host -lt 100;
        do
            ping -c3 192.168.0.$host > /dev/null && echo "192.168.0.$host UP"
            host=$(($host+1))
        done
    else
        # loop through the hosts passed in
        while test $# -gt 0 # while number of arguments is greater than 0
        do
            ping -c3 $1 > /dev/null && echo "$1 UP" || echo "$1 DOWN"
            shift # shift to the next argument, decrement $# by 1
        done
    fi
else
# if the number of arguments is 0, return a message stating invalid input
    echo "No arguments specified. Expected -all or host names/ip addresses."
    echo "Usage: ping: -all"
    echo "Or: ping: 192.168.0.1,192.168.0.16"
fi
Roy
  • 418
  • 2
  • 16
0

On Google Nexus 10, running Android 5.1, a construct like this works as expected:

i=0
while ((i < 3)); do
    echo $i;
    ((i++));
done

However, a construct like this results in an error message being displayed:

for ((i = 0; i < 3; i++)); do
    echo $i;
done
Fara Importanta
  • 161
  • 1
  • 2
  • 10