我尝试使用bash做一个简单的比较,以检查一行是否为空:
line=$(cat test.txt | grep mum )
if [ "$line" -eq "" ]
then
echo "mum is not there"
fi
但它不起作用,它说:[:太多参数
非常感谢你的帮助!
发布于 2010-07-21 23:01:37
您还可以使用设置为命令返回状态的$?
变量。所以你会有:
line=$(grep mum test.txt)
if [ $? -eq 1 ]
then
echo "mum is not there"
fi
对于grep
命令,如果有任何匹配,则将$?
设置为0(完全退出),如果没有匹配,则$?
为1。
发布于 2010-07-21 22:58:56
if [ ${line:-null} = null ]; then
echo "line is empty"
fi
或
if [ -z "${line}" ]; then
echo "line is empty"
fi
发布于 2010-07-21 23:08:53
在bash中也适用的经典sh答案是
if [ x"$line" = x ]
then
echo "empty"
fi
您的问题也可能是您正在使用'-eq‘,它用于算术比较。
https://stackoverflow.com/questions/3300633
复制相似问题