Unlimited choices in BASH case statement -


consider simple bash snippet:

case $option in   1)     image=${options[0]%.tar}     ;;   2)     image=${options[1]%.tar}     ;;   3)     image=${options[2]%.tar}     ;;   4)     image=${options[3]%.tar}     ;;   *)     echo "invalid option"     exit 1 esac 

in real script numbers goes 30. makes pretty long. can somehow specify cases variable?

something this:

case $option in   $i)     image=${options[$(($i-1))]%.tar} 

any pointer appreciated.

you can match against multiple patterns in single clause:

case $option in     1|2|3:        echo "$option one, 2 or three"        ;; esac 

if still typing you, can use simple pattern matching:

case $option in   # following matches   #  - single-digit numbers 0-9   #  - two-digit numbers starting either 1 or 2   #  - number 30   [0-9]|[12][0-9]|30)      image=${options[$(($option-1))]%.tar}      ;;   *)      echo "invalid option" 1>&2      exit 1 esac 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -