Re: Case statement

From: William (fred_at_nosmapherethankyou.com)
Date: 05/31/05


Date: Tue, 31 May 2005 14:44:16 +0000 (UTC)


"Dale Hagglund" <dale.hagglund@gmail.com> wrote:

> W> I'm a bit stuck with the below case statement, I'm looking at
> W> taking input from a user using read command, then evaluating
> W> this string to see if it is either within a certain numeric
> W> range i.e. 100-60000.
>
> W> However when I try to specify a range it's not working.
>
> It's not surprising you're having trouble since a case statement
> doesn't do numeric ranges. The patterns in a case statement are
> patterns for matching strings. Your case statement, using a somewhat
> more conventional indentation, was
>
> case $var in
> [100-60000]) echo Numeric between 100-60000 ;;
> *) echo Something else ;;
> esac
>
> What you thought was a numeric range was actually a character class.
> It matchs a single character, one of "0" through "6". So, the first
> glob pattern matches only if $var is one of strings "0", "1", "2",
> "3", "4", "5", and "6". It's the same matching you'd get if you typed
>
> $ ls [100-60000]
>
> at the command prompt. Note that the simplest form of this pattern is
> [0-6].
>
> When the match against the first pattern fails, the case statement
> checks the next pattern. "*" matches any string, and so succeeds,
> causing the string "Something else" to be echoed.
>
> You need to use the if statement to do what you want.
>
> read var
> if [ $var -ge 100 -a $var -le 60000 ]
> then
> echo Numeric between 100-60000
> else
> echo Something else
> fi
>
> Dale.

Hello Dale,

If I input an alpha character I get (bad number), is there another test I
can use to check for an alpha character being input?

--
Regards
William

Quantcast