简体   繁体   English

麻烦的android bash shell脚本

[英]Trouble with android bash shell script

I keep getting syntax errors with this somewhat basic script whenever running it on android by calling bash ping.sh. 每当在android上通过调用bash ping.sh运行它时,我一直在使用这个基本的脚本获得语法错误。 Currently the error is : command not found ping.sh: line 9: syntax error near unexpected token etc. Here's my script: 目前的错误是: command not found ping.sh: line 9: syntax error near unexpected token etc.这是我的脚本:

    #!/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

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). android shell不是GNU bash shell,而是一个POSIX shell(2.x之前的NetBSD Almquist shell,从3.0开始的MirBSD Korn Shell)。

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

for making it work in POSIX shell, rewriting some lines is needed: 为了使它在POSIX shell中工作,需要重写一些行:

#!/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

On Google Nexus 10, running Android 5.1, a construct like this works as expected: 在运行Android 5.1的Google Nexus 10上,这样的构造按预期工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM