2013-04-19 2 views
1

Я продолжаю получать синтаксические ошибки с помощью этого несколько базового скрипта при запуске его на android, вызывая bash ping.sh. В настоящее время ошибка : command not found ping.sh: line 9: syntax error near unexpected token etc. Вот мой сценарий:Проблема с скриптом оболочки bash android

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

У устройства даже есть пинг e xecutable. используйте 'adb shell' чтобы узнать об этом. – petey

+0

На вашем устройстве нет ping. Только небольшое подмножество того, что вы обычно ожидаете найти в установке linux, действительно находится на устройстве, и оно отличается моделью. Вы не можете рассчитывать гораздо больше, чем ls и rm. –

+0

вам лучше попробовать это с помощью java-кода проверить этот вопрос и его ответы: http://stackoverflow.com/questions/11506321/java-code-to-ping-an-ip-address – petey

ответ

3

андроид оболочка не GNU Баш оболочки, но POSIX оболочки (NetBSD Альмквист оболочки до 2.x, MirBSD Korn Shell от 3,0 года).

[ $1 == "-all" ] is Bashism, for ((host=1; host<100; host++)) - еще один башизм.

за то, что работать в POSIX оболочки, переписывание несколько строк необходимо:

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

На Google Nexus 10, работает Android 5,1, конструкция, как это работает, как ожидалось:

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

Однако , такая конструкция приводит к отображению сообщения об ошибке:

for ((i = 0; i < 3; i++)); do 
    echo $i; 
done 
Смежные вопросы