Bash Script verify user input is not empty and is equal to a value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script verify user input is not empty and is equal to a value
# 1  
Old 01-26-2010
Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry.

I am able to test the input against 1,2, and 3, but when I test agains an empty variable, I get an error (expected unary). Below is what I have so far:

Code:
echo
echo "Which item?"
echo "1) choice 1"
echo "2) choice 2"
echo "3) choice 3"
echo
echo "Enter Choice:"
read -e INPUT
echo
while [ $INPUT != 1 ] && [ $INPUT != 2 ] && [ $INPUT != 3 ] && [ $COUNTER -lt 5 ] || [ -z $INPUT ]; do
        clear
        echo
        echo "You did not enter an appropriate choice.  Please enter 1, 2, or 3."
        echo "1) choice 1"
        echo "2) choice 2"
        echo "3) choice 3"
        echo
        echo "Enter Choice:"
        read -e INPUT
        echo
        let COUNTER++
        echo "Counter =$COUNTER"
done
# PROVIDER VARIABLES
case $INPUT in
1)
        PROV_POOL="choice1";;
2)
        PROV_POOL="choice 2";;
3)
        PROV_POOL="choice 3";;
*)
        STATUS=1
esac
if [ $STATUS == 0 ]
then
        echo "You entered a good value"
else
        echo "You have exceeded the error retry count."
fi


Last edited by pludi; 01-26-2010 at 03:55 PM.. Reason: code tags, please...
# 2  
Old 01-26-2010
If you define your counter, it should get you past your initial problem:

Code:
COUNTER=0

# 3  
Old 01-27-2010
I'm not great with tests so I don't know if this is the best way, but I had the same problem recently and finally got this to work best:
Code:
while  [[ $choice != '1' ]] && [[ $choice != '2' ]] && [[ $choice != '3' ]]; do

# 4  
Old 01-27-2010
Just above the case put these statement

Code:
if [ -z "${choice}" ];then
        echo "Please specify the option\n"
        #Call the function here to loop it again.
fi

# 5  
Old 01-27-2010
Thank you

The quotes around the variable made the difference!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to verify each line in input for specific pattern

In the bash below the out put of a process is written to input. What I am trying to do is read each line in the input and verify/check it for specific text (there are always 6 lines for each file and the specific text for each line is in the description). There will always be 6 lines in each... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

3. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

4. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

5. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

6. UNIX for Advanced & Expert Users

Verify file was sftp'd via bash script

Hello Experts, I have a script that that transfers a file (via sftp) and it works fine but we ran into a snag where the target server asked for the ssh key and the script didn't know what to do. I want to add some logic to this script that at least sends an email that it didn't complete as... (4 Replies)
Discussion started by: Tiberius777
4 Replies

7. Shell Programming and Scripting

Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g. read -p "New user? " user Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead? Thanks! :) (5 Replies)
Discussion started by: JayC89
5 Replies

8. Shell Programming and Scripting

BASH Varible nesting and user input

Well, I think I've managed to take two different issues and conglomerate them into and embarrasing mess. #!/bin/bash # Set some variables dir1=/path/that/isnt/variable/$variabledir/dir/ dir2=/path/that/isnt/variable/$variabledir/important/"$variabledir"-subdirectory/path/ echo "Gimme... (7 Replies)
Discussion started by: karlp
7 Replies

9. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

10. Shell Programming and Scripting

Script error with when input is empty

Hi, I created the following shell script to lookup multiple name servers for a domain. #!/bin/bash echo -n "Enter the domain name and press : " read domain result1=`dig +short $domain @4.2.2.1` revresult1=`host $result1 | cut -f5 -d " "` echo "test1" result2=`dig +short... (3 Replies)
Discussion started by: mohitmoudgil
3 Replies
Login or Register to Ask a Question