Solved

How can I detect the user response in shell script in UNIX by ignoring case?

Posted on 2011-09-28
13
360 Views
Last Modified: 2012-05-12
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,

0
Comment
Question by:Tolgar
  • 7
  • 5
13 Comments
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
ID: 36812724
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

0
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
ID: 36812747
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

0
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
ID: 36812789
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

0
 

Author Comment

by:Tolgar
ID: 36815212
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,




0
 

Author Comment

by:Tolgar
ID: 36815440
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,
0
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
ID: 36815502
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Murugesan Nagarajan
ID: 36815609
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

0
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
ID: 36815651
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

0
 

Author Comment

by:Tolgar
ID: 36815877
I see. How can we write this script for ?

#!/bin/csh

Open in new window


Thanks,



0
 

Author Comment

by:Tolgar
ID: 36823346
any idea?

Thanks,

0
 
LVL 7

Accepted Solution

by:
Murugesan Nagarajan earned 400 total points
ID: 36832600
There is almost no difference between syntax of csh and tcsh statement
from

http://faculty.plattsburgh.edu/jan.plaza/computing/help/tcsh.htm



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




The NEW FEATURES section describes major enhancements of tcsh over csh(1).
Throughout this manual, features of tcsh not found in most csh(1)
implementations (specifically, the 4.4BSD csh) are labeled with `(+)',
and features which are present in csh(1) but not usually documented are
labeled with `(u)'.
On a Linux system I use, csh is a symbolic link to tcsh. On a Solaris box, they are different (i.e. they have different lengths).
from


http://www.unix.com/shell-programming-scripting/65159-differences-between-csh-tcsh.html
0
 
LVL 4

Assisted Solution

by:Anacreo
Anacreo earned 100 total points
ID: 36893717
An improved version of this code...  There is no reason to leave csh for this functionality it can do the mixed case internally.

#!/bin/csh
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 = $<
        switch ($userResponse)
                case [Yy][Ee][Ss]:
                        echo doing your job here.
                        breaksw
                case [Nn][Oo]:
                        echo exiting this script as you have entered "$userResponse"
                        exit
                        breaksw
                case *:
                        echo 'Please use Yes or No as your response.'
                        breaksw
        endsw
end

Open in new window

0
 

Author Closing Comment

by:Tolgar
ID: 36893747
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now