bash string compare to multiple correct values

前端 未结 5 953
时光取名叫无心
时光取名叫无心 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).

提交回复
热议问题