Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bourne if with UPPER or Lower Case 2

Status
Not open for further replies.

Michael42

Programmer
Joined
Oct 8, 2001
Messages
1,454
Location
US
Hello,

Using a Bourne shell script I would like my if statement to work if the variable's value is upper or lower case "Y". What would be the syntax for this?

Note - this did not work for me: if [ $DELETE = "[Yy]" ]; then

Code:
DELETE="Y"

if [ $DELETE = "Y" ]; then
   echo "Deleting stuff..."
fi

Thanks,

Michael42
 
hello,

There may be other ways, but I usually do it like this:
if [ $DELETE = Y -o $DELETE = y ]

btw, you might want to include the $DELETE in "",
just in case it is not set.

hope this helps
 
Hi

I knew it, ther is some basic matching in [tt]bash[/tt] too :
Code:
if [[ $DELETE == [Yy] ]]; then
  echo "Deleting stuff..."
fi
I must use it in the future to not forget it again...

Feherke.
 
But we're not talking bash here, we're talking Bourne Shell (sh).
Does this work in "sh" too?


Trojan.
 
Hi

Good question. Works also when [tt]bash[/tt] is runned as [tt]sh[/tt], so theoretically sould be compatible. I have no Bourne shell to try it properly.

Feherke.
 
Guys - great stuff. :-)



Thanks,

Michael42
 
A most portable way:
case $DELETE in
[yY]*) echo "Delete stuff ...";;
*) : Do nothing;;
esac

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
or you can case-change the variable first:

Code:
DELETE=`echo ${DELETE}|tr '[a-z]' '[A-Z]'`
if [ "${DELETE}" = "Y" ]
then
 echo "Deleting stuff..."
fi

HTH,

p5wizard
 
Why not try:

if [ "${DELETE}" = "Y" ] || [ "${DELETE} = "y" ] ;
then
echo "Do something...."
fi
 
because it's easier to localize feherkes solution:
Code:
if [[ $DELETE == [YyJj] ]]; then
  echo "Deleting stuff..."
fi
[code]

seeking a job as java-programmer in Berlin: [URL unfurl="true"]http://home.arcor.de/hirnstrom/bewerbung[/URL]
 
Yea but he wants to use Bourne Shell, I think that will only work in Bash.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top