今天写脚本时遇到一个错误:
大体如下:
echo "please input you choice(yes/no)"
read input
if [ $input -eq yes ];then
exit 0
fi
在判断是否相等的时候出现了错误,后来改为这里写代码片
echo "please input you choice(yes/no)"
read input
if [ "$input" = "yes" ];then
exit 0
fi
顺利通过。
查了一下,才发现,虽然-eq和=都有判断两边是否相等的功能,但是-eq偏向整数数字,不能进行字符串的测试,=既适用于数字又适用于字符串。从网上粘了一个小测试,功能明了:
$ [ 1 -eq 1 ] && echo "ok"
ok
$ [ 1 = 1 ] && echo "ok"
ok
$ [ "a" -eq "a" ] && echo "ok"
sh: [: a: 需要整数表达式
$ [ "a" = "a" ] && echo "ok"
ok