Re: using case to check boundary in number range

From: Bill Seivert (seivert_at_pcisys.net)
Date: 04/07/05


Date: Wed, 06 Apr 2005 21:22:14 -0600


Chris F.A. Johnson wrote:
> On Wed, 06 Apr 2005 at 09:05 GMT, peter_sands@techemail.com wrote:
>
>>Hi,
>>
>>I read a file that contains various shipment numbers/details. I go thru
>>the file to extract the total number of lines using awk.
>
>
> AWK is probably overkill. Try "wc -l".
>
>
>>I then use that in a menu for the user to select more details on
>>that item. All this works OK. However, I need some error checking
>>within the menu that the use selects the item, by entering the line
>>number. I have :
>>
>>echo -n "select shipment [ 1 .. $total_lines ]"
>>
>>The total lines in the file is always > 1, so I need a way to check the
>>user does not select a number that is greater than $total_lines.
>>
>>I thought using a case statement, but how do I include the numbers
>>between 1 and total _number ?, is there a class I could use .
>
>
> You may want to use a case statement to ensure that the
> user has entered a number, but to check whether the number is
> valid, use if instead of case:
>
>
> case $num in
> *[!0-9]* | "") echo "Invalid entry: ${num:-[NOTHING]}" ;;
> *) if [ $num -lt $total_lines -a $num -ge 1 ]
> then
> : do whatever
> else
> echo "Invalid number: $num"
> fi
> ;;
> esac
>
>
Or:
     # expr will return the number of characters that
     # match the regular expression, or zero.
     isnum=`expr "$num" : '[0-9][0-9]*$'`
     if [ $isnum -eq 0 ] ; then
         echo "Invalid (non-numeric) entry: \"$num\"."
     elif [ $num -gt $total_lines ] ; then
         echo "Entered number $num out of range (1 -> $total_lines)."
     else
         : do whatever with $num.
     fi

Bill Seivert



Relevant Pages