If not equal to then loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If not equal to then loop
# 1  
Old 04-23-2012
If not equal to then loop

How do I go about amending this simple script that prompts for a yes/no response so that if neither Y or N are entered it will loop back back to the original prompt

Code:
#!/bin/ksh
echo "Enter yes of no"
read answer
if [ $answer == y -o $answer == Y ]
then
  echo "You selected yes"
elif [ $answer == n -o $answer == N ]
then
  echo "You selected no"
elif [ $answer != y -o $answer != Y -o $answer != n -o $answer != N ]
then
  echo "Invalid Option"
fi

Thanks
Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use [code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-23-2012 at 05:50 AM.. Reason: code tags
# 2  
Old 04-23-2012
Hi


Code:
while [ 1 ]
do
echo "Enter yes of no"
read answer
if [ $answer == y -o $answer == Y ]
then
echo "You selected yes"
break
elif [ $answer == n -o $answer == N ]
then
echo "You selected no"
break
elif [ $answer != y -o $answer != Y -o $answer != n -o $answer != N ]
then
echo "Invalid Option. Try again"
fi
done

Guru.
# 3  
Old 04-23-2012
Using case statement:
Code:
echo "Enter y/n"
while read answer
do
  case $answer in 
    y|Y) echo "You selected yes" ;;
    n|N) echo "You selected no" ;;
    *)   echo "Invalid Option. Try again"
         continue ;;
  esac
  break
done

# 4  
Old 04-23-2012
Thanks to both of you.

Much appreciated.
# 5  
Old 04-23-2012
You can get this achieved using until loop.

E.g

Code:
#!/bin/ksh
echo "Enter yes of no"
read answer

until [ $answer == y -o $answer == Y -o $answer == n -o $answer == N ]
do
echo "Sorry, Please Enter yes of no"
read answer
done

if [ $answer == y -o $answer == Y ]
then
  echo "You selected yes"
    elif [ $answer == n -o $answer == N ]
      then
          echo "You selected no"
	      elif [ $answer != y -o $answer != Y -o $answer != n -o $answer != N ]
	          then
		        echo "Invalid Option"
			      fi

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use [code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-24-2012 at 12:41 AM..
# 6  
Old 04-24-2012
Thanks for the help on this, how would I expand it to prompt for two responses and loop back to the original prompt

i.e.
Code:
echo "Enter a choice"
read choice 
 
echo "confirm $choice is correct y/n"
read confirm
 
if [ $confirm == y -o $confirm == Y ]
then do something
 
elif [ $confirm == n -o $confirm == N ]
then loop back to the enter a choice prompt
 
elif [ $confirm != y -o $confirm != Y -o $confirm != n -o $confirm != N ]
then
echo "Invalid Option" and loop back to confirm choice

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 04-24-2012 at 04:29 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

awk not equal

Did I do something wrong with this awk not equal? For some reason it prints twice. >awk '{if ($4 != "root") print $1 " " $4 " " $5}' ls_test server10: njs nodeadm server10: njs nodeadm >grep server10 ls_test server10: drwxr-sr-x. 18 njs nodeadm 4096 Aug 16 09:42 /opt > (2 Replies)
Discussion started by: cokedude
2 Replies

2. UNIX for Dummies Questions & Answers

Same strings are not equal

Hi there can anyone help me please. I want to make a program to check if the executable file specified by the user exists in the directory. When I run this program particulary these lines of code does not work: if ("$fi" == "$name") then where It checks whether the specified file is equal to the... (1 Reply)
Discussion started by: FUTURE_EINSTEIN
1 Replies

3. Shell Programming and Scripting

Not equal to in Unix

Guys, I am trying to do below operation LAST_TRANSACTION=2 if ]; then # do something fi If the LAST_TRANSACTION variable is not equal to 1 or 2 or 3 then code inside the if block should be execute. This code is not working, Any help is appreciated. (7 Replies)
Discussion started by: gowrishankar05
7 Replies

4. Shell Programming and Scripting

while [ $x -ge 50 ] + and equal to zero ; then

while + and equal to zero ; then what to punt instead of phrase and equal to zero. it's bash thank you in advance (1 Reply)
Discussion started by: losh
1 Replies

5. Shell Programming and Scripting

My Values are Equal but They are Not

Does anybody understand why this is not being interpreted as true. Script: #!/bin/bash errored=`grep "errored" new_update_scripts.txt` echo $errored = "errored" if ; then echo true else echo false fi Output: $ UpdateScripts errored = errored false (7 Replies)
Discussion started by: scottwmackey
7 Replies

6. Shell Programming and Scripting

equal to operator

Hi, I have the below script executed arg="dir" if "$arg" = "dir" then echo "true" else echo "false" fi Please let me know what happens in the if command. My output is: dir: dir: No such file or directory false which is not the desired output. When i used test command... (1 Reply)
Discussion started by: anijan
1 Replies

7. Shell Programming and Scripting

Regex NOT EQUAL help

I have the following line to text: ExecuteQueue Name=default ThreadCount=60 I want to write a sed or awk function that eliminates everything before "ThreadCount" without taking into account what is actually in front of ThreadCount. Meaning there may be text in front of "ThreadCount" other... (6 Replies)
Discussion started by: ArterialTool
6 Replies

8. UNIX for Dummies Questions & Answers

while loop not taking the not equal to condition

Hi I am trying to write a code like this echo "enter a type" read a_type while || || || || || do echo "invalid engine type Please enter correct a_type" read engine_type < /dev/tty done My problem is that even if i give the a_type as the correct one that i have listed above that... (9 Replies)
Discussion started by: ssuresh1999
9 Replies

9. Solaris

Stop+A equal

Hi, I have replaced my current Intel PC machine with Solaris 10, it use to have windows XP. I am sure alot of people already done this and i have seen Solaris running smoothly but having keyboard problem. What is the equal keys in a QWERTY keyboard for selection <Stop+A> ? Is there a... (5 Replies)
Discussion started by: tlee
5 Replies
Login or Register to Ask a Question