【基本介绍】
在编写shell脚本的时候会经常遇到“line 2: [: too many arguments”类似的错误,特别是在if判断语句中。
【简单案例】
[code="shell"]
# cat bb.sh
tmp="grep Accept /root/test.log | grep -v something"
if [ $tmp ]
then
echo "aa"
else
echo "bb"
fi
# bash bb.sh
bb.sh: line 2: [: too many arguments
bb
[/code]
解决方案:在if判断的变量尽可能的添加双引号
[code="shell"]
# cat bb.sh
tmp="grep Accept /root/test.log | grep -v something"
if [ "$tmp" ]
then
echo "aa"
else
echo "bb"
fi
# bash bb.sh
aa
[/code]
【参考】
[url]http://fengyonghui.iteye.com/blog/568541[/url]
在编写shell脚本的时候会经常遇到“line 2: [: too many arguments”类似的错误,特别是在if判断语句中。
【简单案例】
[code="shell"]
# cat bb.sh
tmp="grep Accept /root/test.log | grep -v something"
if [ $tmp ]
then
echo "aa"
else
echo "bb"
fi
# bash bb.sh
bb.sh: line 2: [: too many arguments
bb
[/code]
解决方案:在if判断的变量尽可能的添加双引号
[code="shell"]
# cat bb.sh
tmp="grep Accept /root/test.log | grep -v something"
if [ "$tmp" ]
then
echo "aa"
else
echo "bb"
fi
# bash bb.sh
aa
[/code]
【参考】
[url]http://fengyonghui.iteye.com/blog/568541[/url]