[: ==: unary operator expected
原创文章标签 [: ==: unary operato 文章分类 运维
©著作权归作者所有:来自51CTO博客作者月永月的原创作品,请联系作者获取转载授权,否则将追究法律责任
#!/bin/bash
ssl=`netstat -ant |grep "0.0.0.0:22"|awk '{print $4}'|awk -F: '{print $2}'`
if [ ${ssl}x = 22x ]
then
echo "ssh is running"
else
echo "the ssh service already stop"
/etc/init.d/sshd start
sleep 4
echo "the ssh service already start"
fi
~
监控ssh 服务是否启动
写成这样就会报错
if [ $ssl = 22 ]
“[: ==: unary operator expected”
这是由于做判断的变量值为空导致的。谷歌出解决方案:
在变量之后加任意字符。例如,要判断变量un是否为auto又要防止un为空出错则这样写
if [ ${un}x == autox ]
当un为auto时,表达式为autox == autox,成立;
un为空,表达式为x == autox ,则不成立。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
line 6: [: 192.168.26.233: unary operator expected
学习shell的时候出了下面一个问题困扰了我一会line 6: [: 192.168.26.233
unary operator expec 双引号 if语句 -
C++ new operator, delete operator, operator new, operator delete, new placementC++ C/C++ 构造函数 c++ 析构函数