Link to home
Start Free TrialLog in
Avatar of Tolgar
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:

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

Open in new window


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,

Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

Here goes the script using "/bin/tcsh" :

#!/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
        endif
end
echo Exiting this script

Open in new window

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

Open in new window

Updated script to agree with any of the following inputs:

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

Open in new window

Avatar of Tolgar
Tolgar

ASKER

but this does not go out of the loop even if I say yes once.

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..

Open in new window


How can I do it?

Thanks,




Avatar of Tolgar

ASKER

btw, When I say this :

>>echo $SHELL

Open in new window


I get

>>/usr/local/bin/tcsh

Open in new window


But I put

#!/bin/csh

Open in new window


in the beginning of all my scripts.

But in your code you put

#!/bin/tcsh

Open in new window


Does it matter?


Thanks,
Updated accordingly:

#!/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

Open in new window


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:

#!/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

Open in new window

Updated script using /bin/ksh
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

Open in new window

Avatar of Tolgar

ASKER

I see. How can we write this script for ?

#!/bin/csh

Open in new window


Thanks,



Avatar of Tolgar

ASKER

any idea?

Thanks,

ASKER CERTIFIED SOLUTION
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

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