The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script getting input from output aspect_p Shell Programming and Scripting 1 03-11-2008 07:37 AM
Problem with shell script...ORA-00904:invalid identifier bhagat.singh-j Shell Programming and Scripting 12 11-13-2006 10:33 AM
Input to shell script castlerock Shell Programming and Scripting 3 04-03-2006 05:26 AM
Masking input in a shell script Spetnik UNIX for Dummies Questions & Answers 2 12-29-2003 03:12 PM
single input shell script? quipy Shell Programming and Scripting 1 10-03-2002 08:18 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-26-2007
rockysfr rockysfr is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 24
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 03:55 AM.. Reason: messy program source code
  #2 (permalink)  
Old 06-26-2007
kamitsin's Avatar
kamitsin kamitsin is offline
Registered User
  
 

Join Date: Nov 2006
Location: /dev/null
Posts: 177
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
  #3 (permalink)  
Old 06-26-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by rockysfr View Post
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0