【发布时间】:2018-01-25 06:52:24
【问题描述】:
我编写了一个 bash 脚本,其工作原理如下
1) Check for status of service1. If it is not started then check for status of service2. If service2 is not started then display error.
2) If service1 is started then check for log file of service1 and grep for `starting`. If it matches then success
3) If service2 is started then check for log file of service1 and grep for `starting`. If it matches then success
我的 bash 脚本如下所示,但它的 if 语句太多。我正在努力想办法减少这么多的 if 语句
#!/bin/bash
service service1 status | grep "good" > /dev/null
if [ $? -ne 0 ];then
service service2 status | grep "good" > /dev/null
if [$? -ne 0];then
var1="service1 is down"
echo "$var1"
else
cat /dir/log2 | grep "abc" > /dev/null 2>&1
if [ $? -ne 0 ];then
var2="exception"
echo "$var2"
fi
fi
else
cat /dir/log1 | grep "abc" > /dev/null 2>&1
if [ $? -ne 0 ];then
var3="exception"
echo "$var3"
fi
fi
【问题讨论】:
-
我在这里没有看到问题.. 因为 if-else 实现了分支,我觉得这是一种有效的方法。
:-)
标签: linux bash service sh rhel