bash string compare to multiple correct values

前端 未结 5 944
时光取名叫无心
时光取名叫无心 2020-12-01 02:41

i have the following piece of bashscript:

function get_cms {
    echo \"input cms name\"
    read cms
    cms=${cms,,}
    if [ \"$cms\" != \"wordpress\" &am         


        
相关标签:
5条回答
  • 2020-12-01 03:07

    As @Renich suggests (but with an important typo that has not been fixed unfortunately), you can also use extended globbing for pattern matching. So you can use the same patterns you use to match files in command arguments (e.g. ls *.pdf) inside of bash comparisons.

    For your particular case you can do the following.

    if [[ "${cms}" != @(wordpress|magento|typo3) ]]
    

    The @ means "Matches one of the given patterns". So this is basically saying cms is not equal to 'wordpress' OR 'magento' OR 'typo3'. In normal regular expression syntax @ is similar to just ^(wordpress|magento|typo3)$.

    Mitch Frazier has two good articles in the Linux Journal on this Pattern Matching In Bash and Bash Extended Globbing.

    For more background on extended globbing see Pattern Matching (Bash Reference Manual).

    0 讨论(0)
  • 2020-12-01 03:14

    Instead of saying:

    if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then
    

    say:

    if [[ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]]; then
    

    You might also want to refer to Conditional Constructs.

    0 讨论(0)
  • 2020-12-01 03:18

    Maybe you should better use a case for such lists:

    case "$cms" in
      wordpress|meganto|typo3)
        do_your_else_case
        ;;
      *)
        do_your_then_case
        ;;
    esac
    

    I think for long such lists this is better readable.

    If you still prefer the if you can do it with single brackets in two ways:

    if [ "$cms" != wordpress -a "$cms" != meganto -a "$cms" != typo3 ]; then
    

    or

    if [ "$cms" != wordpress ] && [ "$cms" != meganto ] && [ "$cms" != typo3 ]; then
    
    0 讨论(0)
  • 2020-12-01 03:21

    If the main intent is to check whether the supplied value is not found in a list, maybe you can use the extended regular expression matching built in BASH via the "equal tilde" operator (see also this answer):

    if ! [[ "$cms" =~ ^(wordpress|meganto|typo3)$ ]]; then get_cms ; fi
    

    Have a nice day

    0 讨论(0)
  • 2020-12-01 03:23

    Here's my solution

    if [[ "${cms}" != +(wordpress|magento|typo3) ]]; then
    
    0 讨论(0)
提交回复
热议问题