mardi 7 juin 2016

How to test if a variable's content is in a set of values?

I created two directories under /tmp/: A and B.

I want to pass each directory name as parameter to a shell script and execute some commands in said directory, as in below script:

#!/bin/sh

if [ ! -z "$1" ]
then
        cd /tmp/$1
        pwd
        ls
fi

Now I want to prevent the script to execute when the parameter given is not either A or B. That is, on attempt to run ./script.sh C it should produce something like invalid parameter.

How can I test if the variable's content is in the set of allowed values?

Aucun commentaire:

Enregistrer un commentaire