In my bash script I want to say "if argument 2 is anything except x, y or z, than echo this" (x y and z being words).
So my script looks like this:
if [[ $2 != {x,y,z} ]]
then
echo "unrecognized input: $2"
fi
This usually works but than I also want to say "if argument 2 IS x, y, or z, but argument 4 is not one of them, echo 'unrecognized input: $4'". So how to I format this so my script does both. I had this, but whenever argument 2 was x, y or z, and argument 4 wasn't, I still got the "unrecognized input: $2" error message.
if [[ $2 != {x,y,z} ]]
then
echo "unrecognized input: $2"
elif [[ $2 = {x,y,z} && $4 != {x,y,z} ]]
then
echo "unrecognized input: $4"
fi
You don't need to re-test $2 in line 4, as you have already determined that it is one of x, y or z in line 1.
If $2 or $4 is null, the script will die.
Thanks for the advice but I still couldn't get it to quite do what I want it to do. I have added in a loop after some advice from a computer scientist. The updated version of the problematic section of my code:
list="inches hands feet yards furlongs miles leagues au"
for item in $list
do
if [[ "$2" = "$item" ]]
then
if [[ "$4" != "$item" ]]
then
echo "unrecognized unit: $4"
fi
else
echo "unrecognized unit: $2"
fi
done
This of course produces 8 error messages for any input where either $2 or $4 is not part of $list. So to put it in english, this is what I want to do: "go through the whole list and if ANY match with $2, check $4, if none match, produce 1 error message (unrecognized unit: $4). Also if nothing in the list matches with $2, produce 1 error message (unrecognized unit: $4)". What do I need to do/change to get my code to do this.