zoukankan      html  css  js  c++  java
  • [shell test] multiple conditions

     

    Classic technique (escape metacharacters):

    if[ ( $g -eq 1-a "$c"="123" ) -o ( $g -eq 2-a "$c"="456" ) ]then echo abc
    else echo efg
    fi

    I tried various tricks with '[[ ... ]]' without success - even escaping the parentheses did not seem to work.


    Isn't it a classic question?

    I would have thought so. However, there is an alternative, namely:

    if[ $g -eq 1-a "$c"="123"]||[ $g -eq 2-a "$c"="456"]then echo abc
    else echo efg
    fi

    Indeed, if you read the 'portable shell' guidelines for the autoconf tool or related packages, this notation - using '||' and '&&' - is what they recommend. I suppose you could even go so far as:

    if[ $g -eq 1]&&["$c"="123"]then echo abc
    elif[ $g -eq 2]&&["$c"="456"]then echo abc
    else echo efg
    fi

    Where the actions are as trivial as echoing, this isn't bad. When the action block to be repeated is multiple lines, the repetition is too painful and one of the earlier versions is preferable.

  • 相关阅读:
    Python 教程之String
    python 斐波纳契数列实现
    js 中 document.createEvent的用法
    C#里调用 MysqlDB
    [网购]
    [ENLearning] 2010920
    [EN Learning] 2010913
    [EN Learning] 2010910
    [ENLearning] 2010921
    [EN Learning] 2010916
  • 原文地址:https://www.cnblogs.com/zaric/p/3412645.html
Copyright © 2011-2022 走看看