I’m writing a script right now in Bash that will execute two SQL queries and analyze that queried data. One of the command line inputs takes in an environment variable, which can be one of three values. If it’s not one of those values, the script displays a message that prompts the user to enter in a correct value. However, the script doesn’t properly check the value, instead prompting the user. Here is my code:
if [[ -z $ENV1 || $ENV1 != "ITF" || $ENV1 != "Prod" || $ENV1 != "QA" ]]
then
read -rp "Please enter the first environment (ITF, Prod, QA): " ENV1
fi
echo $ENV1
I think it’s a problem with having multiple ||’s in the if line. How can I go about checking for all for of those conditions?
It looks to be a problem with your condition. Even if
ENV1is any of your options, one of the conditions will be true. For example,ENV1could be “QA”, butENV1 != "Prod"will still evaluate to true (0). Instead of||use&&: