【问题标题】:Replacing too many if statements in bash替换 bash 中的太多 if 语句
【发布时间】: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


【解决方案1】:

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

关注blog 可能会对您有所帮助。

【讨论】:

    【解决方案2】:

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

    测试本身也有一些简化。 somecommand; if [ $? -ne 0 ]; then(否定测试——如果命令失败则运行)可以替换为if ! somecommand; then(或if somecommand; then,用于肯定测试版本)。此外,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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多