【发布时间】:2015-06-14 07:38:22
【问题描述】:
有人可以帮忙告诉我为什么这不起作用吗?
我检查了脚本并解决了一些问题,但它仍然不能正常工作,我找不到错误。
#!/bin/bash
#variabelen
IP="192.168.0."
array=($@)
#functies
数组有什么问题?我的 linux 告诉我第 12 行有一个语法错误,所以使用那个数组,但他没有告诉我是什么。
function Sorteer(){
array=($(printf '%s\n' "$@"|sort -nu))
for i in ${array[@]};do
ping -c -1 "IP"$i
done
}
function Telbij(){
# given number $i +200
b=$(( $i + 200 ))
if (( b > 255 ))
then
echo "Neem kleiner getal";
else
ping -c 1 "IP"$b;
fi
}
function XXYY() {
#ping 65-68 = ping 65 66 67 68
start=${1%-*}
end=${1#*-}
for ((i=start;i<=end;i++));do
ping -c 1 "$IP"$i
done
}
错误在 if else 函数中:http://prntscr.com/7gr8yf
但我不知道这是什么意思:“提到的解析器错误在这个 else 子句中。”
if [ "$#" -eq 0 ]; then
echo "Er moet minimaal 1 parameter worden meegegeven "
exit 0
else
case
-h -help ) echo "Geef de laatste cijfers van het IP-adres van de pc's om te testen.";;
XX-YY ) XXYY;;
-t ) Telbij;;
- sort ) Sorteer;;
esac
fi
done
【问题讨论】:
-
抱歉,不,如果您不说出实际问题是什么,我们将无法提供帮助。 “它仍然不能正常工作”很模糊,你不觉得吗?告诉我们这种行为有什么问题。 要具体!
-
在调试阶段将第一行临时替换为
#!/bin/bash -vx。你会得到一些痕迹。 -
另一个有用的shell调试工具:shellcheck.net
-
谢谢,我会先使用 shellcheck.net。
-
如果您使用
sh scriptname运行 Bash 脚本,您将收到数组初始化的语法错误。使用sh运行脚本时,您不能使用 Bash 结构,即使sh是bash的符号链接(在这种情况下,Bash 将以 POSIX 兼容模式运行)。这是一个大量的常见问题;请参阅bash tag wiki 中的第 2 项。
标签: linux shell ubuntu scripting