using OR in IF loop

Hello,

Do we have an availability to use OR(|) in IF loop for comparision

Ex: I have script which will take one arugument..so while running i will give as

./example.sh one

In code I have taken this arugument in variable and have to compare this with another like

if( a = one | two)
then
a=fllower
else 
a=book
fi

I am unable to use the OR (|) in IF loop in shell script....can anyone help me please

Thanks in advance

Posix shell:

if [ "$a" = one ] || [ "$a" = two ] ; then
...
fi

Another way with ksh or bash (with extglog option set) :

if [[ "${a}" == @(one|two) ]]

Jean-Pierre.

1 Like

Just to correct the typo, its extglob

You can enable it with shopt -s extglob and disable shopt -u extglob

Other useful options are:

?(pattern-list)
	Matches zero or one occurrence of the given patterns
*(pattern-list)
	Matches zero or more occurrences of the given patterns
+(pattern-list)
	Matches one or more occurrences of the given patterns
@(pattern-list)
	Matches one of the given patterns
!(pattern-list)
	Matches anything except one of the given patterns

Thanks to aigles for the post which forced me to dig more and learned something new :slight_smile:

you can use -o for OR and -a for AND