PDA

View Full Version : Bash conditionals



Mevsthevoices
March 11th, 2008, 05:34 AM
I was wondering if bash has some way of comparing 1 string to many others using logical OR
Say like
if [ "$readvar" = 'y' || 'Y' ];then
echo 'Yay'
fi

Would it be ok to use square brackets? say
if [ "$readvar" = [yY] ];then
echo 'Yay
fi

mssever
March 11th, 2008, 06:28 AM
You can use limited regex functionality, but if you do this, you must use #!/bin/bash, not #!/bin/sh.

if [[ "$somevar" =~ [yY] ]]; then
#do stuff
fiI'm not 100% positive of the syntax, but it's similar.

EDIT: You could also keep it basic and so something like

if [ "$somevar" = y -o "$somevar" = Y ]; then
#do stuff
fi
-o means or. -a means and. ! means not

ghostdog74
March 11th, 2008, 07:43 AM
use case, more neat, IMO


case $variable in
y|Y ) echo "yes ";;
esac

Mevsthevoices
May 5th, 2008, 08:35 AM
Thanks that worked perfect