Solved

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

Posted on 2011-09-28
13
361 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What are recommended OS for exim mail server? 10 111
unix in java example 9 65
Solaris 4.1.3 cloning and booting 13 79
awk sed 8 46
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

832 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