Linux 替换bash中过多的if语句

Linux 替换bash中过多的if语句,linux,bash,service,sh,rhel,Linux,Bash,Service,Sh,Rhel,我编写了一个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

我编写了一个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

您可以在bash中使用case语句来避免使用多级if-else语句


以下内容可能会对您有所帮助。

我对您尝试测试的确切逻辑有点困惑——描述与下面的代码不太匹配。我怀疑问题的一部分是做阴性测试(即,如果没有发现什么东西,那么…)可能会令人困惑。例如,描述说“如果[开始]匹配,那么成功”,但代码是“如果abc不匹配,那么varN=exception”。此外,如果service2正在运行,它会检查log1,如果service1正在运行,它会检查log2。我建议切换到阳性测试(即,如果发现了什么,那么…),并添加注释以跟踪哪些代码在什么条件下运行

测试本身也有一些简化<代码>命令;如果[$?-ne 0];然后(一个否定测试——如果命令失败,它将运行)可以替换为
if!somecommand;然后
(如果是somecommand,则为;对于阳性测试版本,则为)。另外,
grep something>/dev/null
可以替换为
grep-q something
(其优点是它不会扫描整个文件,它会在第一次匹配时停止),而
cat somefile | grep something
可以替换为
grep something somefile
(保存一个进程并提高效率)

将测试转换为阳性形式,并进行上述简化,同时进行我认为必要的更正,得出以下结论:

#!/bin/bash

if service service1 status | grep -q "good"; then
    # This section executes if service1 is "good"
    if grep -q "abc" /dir/log1 2>/dev/null; then
        # service1 is "good" and /dir/log1 contains "abc"
        # Should there be something executable here? If not, add "!"
        # after the "if", and swap the "else" section here
        : success
    else
        # service1 is "good" and /dir/log1 does not contain "abc"
        # Is this where the "exception" stuff actually belongs?
        var3="exception"
        echo "$var3"
    fi

elif service service2 status | grep "good"; then
    # This section executes if service1 is not "good", but service2 is
    if grep -q "abc" /dir/log2 2>/dev/null; then
        # service2 is "good" and /dir/log2 contains "abc"
        : success
    else
        # service1 is "good" and /dir/log2 does not contain "abc"
        # Again, is this where the "exception" stuff actually belongs?
       var2="exception"
        echo "$var2"
    fi

else
    # Neither service1 nor service2 is "good"
    var1="service1 is down"
    echo "$var1"

fi

我看这里没有问题。。就像其他实现分支的方法一样,我觉得这是一种有效的方法<代码>:-)