Shell Script Menus - Rejecting invalid input (KSH)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script Menus - Rejecting invalid input (KSH)
# 1  
Old 06-26-2007
Shell Script Menus - Rejecting invalid input (KSH)

Greetings all,

I'm currently writing a shell script menu which is dynamically populated from an array. Have a question to ask about the filtering of invalid input. I'm using KSH.

A brief description of my algorithm is as follows:

1) Read in input from user and store in a variable. (a valid input would be a numerical value)
2) Use an if conditional to determine if this variable if smaller than the total number of elements in an array. If so, print out some value and break out of the infinite while loop (a while true loop). Otherwise, print error statement and let loop iterate again.

The code is as follows:

Quote:
while true
do

if [$choice -lt ${#sysNames[*]} ]
then

echo "something"
break

else

echo "Invalid. Please retry"

fi

done
This code works if I enter numerical values. However, the program always executes the if-then portion of the code when i key in some garbage as input (ie. strings, characters). How can I re-write the code such that alphabetical chars and strings would get thrown to the else portion?

Thanks in advance.

Last edited by rockysfr; 06-26-2007 at 04:55 AM.. Reason: messy program source code
# 2  
Old 06-26-2007
Put the if statement in the case structure. Something like the code shown below.

Code:
echo enter the value
read val
case $val in

           [0-9]*) if [[ $val -eq $something ]]
           then
           echo $val
           fi;;

           *) echo "Invalid entry" ;;
esac

kamitsin
# 3  
Old 06-26-2007
Quote:
Originally Posted by rockysfr
Greetings all,

I'm currently writing a shell script menu which is dynamically populated from an array. Have a question to ask about the filtering of invalid input. I'm using KSH.

A brief description of my algorithm is as follows:

1) Read in input from user and store in a variable. (a valid input would be a numerical value)
2) Use an if conditional to determine if this variable if smaller than the total number of elements in an array. If so, print out some value and break out of the infinite while loop (a while true loop). Otherwise, print error statement and let loop iterate again.

The code is as follows:
Code:
while true
do

   if [$choice -lt ${#sysNames[*]} ]
   then

       echo "something"
       break

   else

       echo "Invalid. Please retry"

   fi

done


This code works if I enter numerical values. However, the program always executes the if-then portion of the code when i key in some garbage as input (ie. strings, characters). How can I re-write the code such that alphabetical chars and strings would get thrown to the else portion?

Code:
while true
do
   read choice
   case $choice in
      *[0-9]*) echo "Invalid. Please retry"; continue ;;
   esac

   if  [ $choice -lt ${#sysNames[*]} ]
   then
       echo "something"
       break
   else
       echo "Invalid. Please retry"
   fi
done


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Invalid option errors running shell script

The script below fails with the following error messages: gzip: invalid option -- 'w' Try `gzip --help' for more information. mysqldump: Got errno 32 on write cp: invalid option -- 'w' Try `cp --help' for more information. rm: invalid option -- 'w' Try `rm --help' for more information. ... (1 Reply)
Discussion started by: ANNACTION
1 Replies

2. Shell Programming and Scripting

How to find invalid URL in a text file using shell script?

How to find and remove invalid URLs in a text file using shell script? (1 Reply)
Discussion started by: vel4ever
1 Replies

3. Shell Programming and Scripting

Need to get return code from mutt if an address is invalid/undeliverable from Unix shell script

I am using mutt on ksh Unix to send emails to addresses plucked from the database. If the "To:" email address is not longer valid and so the email is not sent to the "To:" recipient, but is sent to the valid cc address, I need to be able to get an error code returned to the shell script so that... (3 Replies)
Discussion started by: jzuber
3 Replies

4. Shell Programming and Scripting

Enter an input and reference another line in ksh script

Hi I have a file like so: Code: Frank Peter Tony Robert Mike 1 2 3 4 5 5 4 2 3 1 4 3 1 5 2 My out should look like this: Peter Tony Mike and so on.... I have the first part done to ask the user to... (8 Replies)
Discussion started by: bombcan1
8 Replies

5. Shell Programming and Scripting

Unzip the input file using shell script (ksh)

Hi, I need help in unziping input file through shell script. I had written script, which checks for input file extention. If Extension is "zip" or "gz", then I want to do unzip/uncompress that file. Caould you please let me know that, How to unzip a file through shell script (ksh). Thanks... (16 Replies)
Discussion started by: Poonamol
16 Replies

6. Shell Programming and Scripting

writing shell script to find line of invalid characters

Hi, I have to write s script to check an input file for invalid characters. In this script I have to find the exact line of the invalid character. If the input file contain 2 invalid character sat line 10 and 17, the script will show the value 10 and 17. Any help is appreciated. (3 Replies)
Discussion started by: beginner82
3 Replies

7. UNIX for Advanced & Expert Users

Shell menus And Oracle

Dear All, Kindly suggest on how should i proceed with the following requirement I need to develop an interactive shell script menu which would enable the user to inquire the value of a column based on a key value . The output can be more records.. Also is it possible to do the following 1)... (2 Replies)
Discussion started by: ksm
2 Replies

8. Shell Programming and Scripting

Menus in Korn Shell and invalid selections

Hey Guys. I need to code a series of menus that have four options, selectable either by the number in the menu or the name, in succession. This part I have achieved however I am struggling to find a way that should the user try to enter an invalid selection, such as the number 5 or an incorrect... (5 Replies)
Discussion started by: Mudja
5 Replies

9. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

10. Shell Programming and Scripting

Problem with shell script...ORA-00904:invalid identifier

Guys, Please suggest me what's wrong with this script? #!/usr/bin/sh ############################################################################## # Author : Bhagat Singh # # # Date : Nov 13,2006 #... (12 Replies)
Discussion started by: bhagat.singh-j
12 Replies
Login or Register to Ask a Question