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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interactive shell script case insensitivity 1

Status
Not open for further replies.

womp

Technical User
Apr 6, 2001
105
US
Hello,
I am trying to create a bash shell script that is interactive.
A question is asked and an answer (Y or N) is required from the user.
How can I make it so the answer given can either be a Y or N
or a y or n?
Here is the snippet I am having difficulties on:

echo "Do you want to add users to this department? "
read YN
if [ $YN = "Y" ]
then
ADDUSERS
fi
How do I put in the script that the answer can be either Y or y or N or n?
 
One way:
echo "Do you want to add users to this department? "
read YN
case $YN in [yY]*) ADDUSERS;; esac

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another way...
Code:
#!/bin/ksh

typeset -u YN

echo "Do you want to add users to this department? "
        read YN
                if [ $YN = "Y" ]
                then
                ADDUSERS
                fi
With that [tt]typeset[/tt], no matter what they type, it comes in as all upper case, so you just test for upper case 'Y'.
 
But still, a case construct is better, because users can type y, yes, Y, YES or whatever...

Code:
typeset -u YN

echo "Do you want to add users to this department? "
        read YN
                case "$YN" in
                 Y*)
                  ADDUSERS;;
                esac



HTH,

p5wizard
 
or using your current method

echo "Do you want to add users to this department? "
read YN
if [ $YN = "Y" -o $YN = "y" ]
then
ADDUSERS
fi

-o = or

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top