Tolgar
asked on
How can I detect the user response in shell script in UNIX by ignoring case?
Hi,
I use tcsh shell and I wanna do something like this:
If YES (ignore case) do smething else if NO (ignore case) do somethig else. If the user enters some other thing then ask the question again. and if the user does not enter anything then just wait for the answer.
How can I do it?
Thanks,
I use tcsh shell and I wanna do something like this:
set userResponse
while ($userResponse == .................... "")
echo -n "Are you sure? "
set userResponse = $<
end
if (userResponse ............YES...........)
do something
exit 0
else if (userResponse ............NO..........)
do something else
exit 1
endif
If YES (ignore case) do smething else if NO (ignore case) do somethig else. If the user enters some other thing then ask the question again. and if the user does not enter anything then just wait for the answer.
How can I do it?
Thanks,
Updated script having only correct endif statement:
#!/bin/tcsh
set userResponse=""
echo -n "Are you sure that you want to execute this script.To continue:yes ? "
set userResponse = $<
while ( ( $userResponse == "yes" ) || ( $userResponse == "YES" ))
echo In the while loop
set userResponse=""
while ( ( $userResponse != "yes" ) && ( $userResponse != "YES" ) && ( $userResponse != "no" ) && ( $userResponse != "NO" ) )
echo -n "Are you sure to continue in while loop [yes/no/YES/NO? "
set userResponse = $<
if ( ( $userResponse != "yes" ) && ( $userResponse != "YES" ) && ( $userResponse != "no" ) && ( $userResponse != "NO" ) ) then
echo You have given Invalid input
endif
end
end
echo Exiting this script
Updated script to agree with any of the following inputs:
YES
YEs
YeS
yES
Yes
yEs
yeS
yes
NO
nO
No
no
YES
YEs
YeS
yES
Yes
yEs
yeS
yes
NO
nO
No
no
#!/bin/tcsh
set userResponse=""
echo -n "Are you sure that you want to execute this script.To continue: YES/YEs/YeS/yES/Yes/yEs/yeS/yes ? "
set userResponse = $<
while ( 1 )
echo $userResponse | egrep -i "yes" > /dev/null
if ( $? == 0 ) then
echo In the while loop
else
echo Exiting this script_here
exit 0
endif
set userResponse=""
while ( 1 )
echo -n "Are you sure to continue in while loop [YES/YEs/YeS/yES/Yes/yEs/yeS/yes/NO/nO/No/no? "
set userResponse = $<
echo $userResponse | egrep -i "yes|no" > /dev/null
if ( $? == 0 ) then
echo $userResponse | egrep -i "no" > /dev/null
if ( $? == 0 ) then
echo Exiting this script_HERE
exit 0
endif
else
echo You have given Invalid input
endif
end
end
ASKER
but this does not go out of the loop even if I say yes once.
What I want is :
How can I do it?
Thanks,
What I want is :
Once I say yes, I want it to do something and then exit the code.
If no, directly exit the code.
If anything else, prompt the user and tell the user that you have given invalid input and please try again..
How can I do it?
Thanks,
ASKER
btw, When I say this :
I get
But I put
in the beginning of all my scripts.
But in your code you put
Does it matter?
Thanks,
>>echo $SHELL
I get
>>/usr/local/bin/tcsh
But I put
#!/bin/csh
in the beginning of all my scripts.
But in your code you put
#!/bin/tcsh
Does it matter?
Thanks,
Updated accordingly:
This script handles all types of input like the following:
Yes/NO
No/YeS
#!/bin/tcsh
set userResponse=""
while ( 1 )
echo -n "Are you sure that you want to execute this script.To continue: [case in sensitive] YES/NO ? "
set userResponse = $<
echo "$userResponse "
echo "$userResponse" | egrep -i "yes" | egrep -v -i no > /dev/null
if ( $? == 0 ) then
echo doing your job here.
exit 0
endif
echo "$userResponse" | egrep -w -i "no" | egrep -v -i yes > /dev/null
if ( $? == 0 ) then
echo exiting this script as you have entered "$userResponse"
exit 0
else
echo You have given Invalid input
endif
end
This script handles all types of input like the following:
Yes/NO
No/YeS
Beginning of the script can be any of the available scripts
However the script language is dependent on the name of the script which is written in the first line of the script.
I always use /bin/ksh
Eg:
However the script language is dependent on the name of the script which is written in the first line of the script.
I always use /bin/ksh
Eg:
#!/bin/ksh
userResponse=""
while [ 1 ]
do
echo -n "Are you sure that you want to execute this script.To continue: [case in sensitive] YES/NO ? "
read userResponse
echo "$userResponse" | egrep -i "yes" | egrep -v -i no > /dev/null
if [ $? = 0 ]
then
echo doing your job here.
exit 0
fi
echo "$userResponse" | egrep -w -i "no" | egrep -v -i yes > /dev/null
if [ $? = 0 ]
then
echo exiting this script as you have entered "$userResponse"
exit 0
else
echo You have given Invalid input
fi
done
Updated script using /bin/ksh
using -eq instead of using =
using -eq instead of using =
#!/bin/ksh
userResponse=""
while [ 1 ]
do
echo -n "Are you sure that you want to execute this script.To continue: [case in sensitive] YES/NO ? "
read userResponse
echo "$userResponse" | egrep -i "yes" | egrep -v -i no > /dev/null
if [ $? -eq 0 ]
then
echo doing your job here.
exit 0
fi
echo "$userResponse" | egrep -w -i "no" | egrep -v -i yes > /dev/null
if [ $? -eq 0 ]
then
echo exiting this script as you have entered "$userResponse"
exit 0
else
echo You have given Invalid input
fi
done
ASKER
I see. How can we write this script for ?
Thanks,
#!/bin/csh
Thanks,
ASKER
any idea?
Thanks,
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
400 for Expert Comment By: murugesandins : For his quick and prompt response and good response for followup questions.
100 for Expert Comment By: Anacreo for the improved version of the code
100 for Expert Comment By: Anacreo for the improved version of the code
Open in new window