UNIX script -- case

i could like to try when i input a integer , the program would give the result i want but it doesnt .

I input 2 , and the result should be "it is greater than or equal to one"

could any ching give me improvement ? If i must use case command .
thank you much !

Use an if statement for this

Why must you use the case construct? This smells like homework. If this were a "real world" requirement, case is not required here, an if/else would do the job.

In any case, your syntax is totally invalid.

..that is not homework . I know how to do it in using if command . I know there are a lot of kids looking for stuff and they dont wanna pay for it .

#!/bin/sh
if [ $1 -ge 1 ]
then
echo "it is greater than or equal to 1"
else
echo " it is little than 1"
fi

---------- Post updated at 05:35 AM ---------- Previous update was at 05:34 AM ----------

i said I must use case command because i wanna try sth new and whether it is possible .
Program needs new creation* , right ?

UNIX (and its shells) provides appropriate tools for a myriad of tasks. In this instance, the if/else construct is appropriate.

Try a different example using case if you want to learn how it works - for example having a user answer yes/no to a question and then having case decide what to do based upon the user response.

ok..um thx .
i will ..um make more practices and learn more .

zazzybob is correct... I think you have to keep in mind case works a bit like grep: it tried to match a variable with given values that means patterns vs conditions
And so what you are trying will not work...

You could of course use trickery and process the number as if it were a string and do something like this:

case $1 in  
  [1-9]*) echo greater than or equal to 1 ;;
       *) echo less or equal ;;
esac

That would work if $1 is always a number, but it would make the code a bit less straight forward / more difficult to read and understand...

oh...that is nice ! um.. I am still a beginner. Is there a logical problem ?
Is [1-9]* = * ? If yes , whatever input , two arguments will be output

---------- Post updated at 06:08 AM ---------- Previous update was at 06:01 AM ----------

i try it and it is work . thanks a lot .
that little strange for me [1-9]* is not equal to *

[1-9]* is not equal to * , it is a unix pattern that matches strings that start with 1,2,3,4,5,6,7,8 or 9

Thank you for you patient.
But i still have some questions .
As u say , asterisk matches 1 to 9 , even any variables .
Why the result is not

less or equal

but first argument ?
um..that is suprise me the program is totally fit , second argument is out when zero or negative values is input.

Because the first pattern matches ( [1-9]* ), the second pattern ( * ) does not get evaluated.

1 Like

o.yes..! That means if
*) echo .... ;;
places first
result must be the first one whatever input is .
Thank you much u telling me that .!
thank you

As scrutinizer stated, the point is that [1-9]* is interpreted by the shell as a unix pattern ( matching any string starting with a figure from 1 to 9)

And NOT as a regular expression (matching 0 or more figures that are in the interval from 1 to 9 )

You can easily test it with empty string or strings like "9a" an check whether it matches or not

scrutinizer remines me that when a program runs ,
read each line --> expands meta-characters ---> execute built-in commands
(if steps are wrong or not precise enough , Please ! tell me)
Are those steps related why the case command program running right (precondition is that only integers input) ? Am i right ?