#!/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 ,则不成立。