Solved

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

Posted on 2011-09-28
13
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:Murugesan Nagarajan
Comment Utility
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
Comment Utility
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
Comment Utility
I see. How can we write this script for ?

#!/bin/csh

Open in new window


Thanks,



0
 

Author Comment

by:Tolgar
Comment Utility
any idea?

Thanks,

0
 
LVL 7

Accepted Solution

by:
Murugesan Nagarajan earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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

17 Experts available now in Live!

Get 1:1 Help Now