android bash shell 脚本的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16108940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble with android bash shell script
提问by linucksrox
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:
每当通过调用 bash ping.sh 在 android 上运行这个有点基本的脚本时,我都会不断收到语法错误。目前的错误是: 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 [ == "-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 > /dev/null && echo " UP" || echo " 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
回答by Roy
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 [ = "-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 > /dev/null && echo " UP" || echo " 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
回答by Fara Importanta
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

