bash check if value in list
Posted
by
tkoomzaaskz
on Stack Overflow
See other posts from Stack Overflow
or by tkoomzaaskz
Published on 2014-06-04T09:22:48Z
Indexed on
2014/06/04
9:24 UTC
Read the original article
Hit count: 172
I've got a script with a variable taken from command line parameters. I want to check if it's value is one of dev
, beta
or prod
. I've got following code snippet:
#!/usr/bin/env bash
ENV_NAME=$1
echo "env name = $ENV_NAME"
ENVIRONMENTS=('dev','beta','prod')
if [[ $ENVIRONMENTS =~ $ENV_NAME ]]; then
echo 'correct'
exit
else
echo 'incorrect'
exit
fi
When I run my script, it doesn't matter which parameters I pass: ./script.sh beta
or ./script.sh
or ./script.sh whatever
, I always get correct
echoed. What is wrong in my script?
© Stack Overflow or respective owner