您的位置:首页 > 运维架构 > Shell

如何在Shell中通过脚本判断某个服务是否存在的解决方法

2008-09-02 14:15 841 查看
创建一个test.sh文件,通过chmod a+x test.sh设置该脚本可以执行
#!/bin/sh
#Filename: test.sh
mysqlsvr=mysql
exist=`chkconfig | grep $mysqlsvr`
if [ -n $exist ];then
echo "exist:
fi

然后执行test.sh的时候,在第五行总是出现提示: [:mysql: binary operator excepted

然后我修改实现脚本为如下,就可以
#!/bin/sh
#Filename: test.sh
if [ $# -lt 1 ];then
echo "please input server name."
exit 1
fi
chkconfig | grep $1 | {
while read svr status;
do
if [ $svr = $1 ];then
echo "server: $svr exist"
fi
done
}

执行: test.sh mysql 得到的结果为: server: mysql exist
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: