UNIX script -- case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX script -- case
# 8  
Old 02-01-2013
You could of course use trickery and process the number as if it were a string and do something like this:

Code:
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...

Last edited by Scrutinizer; 02-01-2013 at 07:06 AM..
# 9  
Old 02-01-2013
Quote:
Originally Posted by Scrutinizer
You could of course use trickery and process the number as if it were a string and do something like this:

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

That would work, but it would make the code 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 ----------

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

Code:
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...
i try it and it is work . thanks a lot .
that little strange for me [1-9]* is not equal to *
# 10  
Old 02-01-2013
[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
# 11  
Old 02-01-2013
Quote:
Originally Posted by Scrutinizer
[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
Code:
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.
# 12  
Old 02-01-2013
Because the first pattern matches ([1-9]*), the second pattern (*) does not get evaluated.
This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 02-01-2013
Quote:
Originally Posted by Scrutinizer
Because the first pattern matches ([1-9]*), the second pattern (*) does not get evaluated.
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
# 14  
Old 02-01-2013
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

2. Shell Programming and Scripting

Help on case to call recursively in UNIX Shell Script

Hi, I am New to Unix Shell Scripting basically, i need some help in achieving a case statement in Shell script to call recursively That is if case having like 1 2 3 4 options , if user inputs 1 and gets executed case should ask for options again but user should not input the same input value 1,... (7 Replies)
Discussion started by: karthikram
7 Replies

3. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

4. Shell Programming and Scripting

Nested Case in UNIX script

Hi I wanted to know if we can write a nested case in UNIX script. Something like following - Case ${sDB} in Srvr1) case ${sSchema} Sch1) DBusr=Username1 DBPwd=Pwd1 ;; Sch2) DBusr=Username2 ... (1 Reply)
Discussion started by: sumeet
1 Replies

5. Shell Programming and Scripting

Help needed in switch case handling in UNIX

Hi, In below code, i am expecting the output has Bye Bye But i am getting has Bye Hi Code: #!/usr/bin/bash var="Hi" cat txt.txt | while read var1 do next="Bye" case $var in Hi) (1 Reply)
Discussion started by: Balamani
1 Replies

6. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

7. UNIX for Dummies Questions & Answers

Clear Case views on UNIX

Friends, I was asked to work on Clear Case after setting up. For this created a dynamic view by using the command, 'cleartool mkview -tag <view name> -stgloc viewstg'. Now I am not sure how to proceed further :-( May I request you to help me out in continuing further. I have a deadline in... (1 Reply)
Discussion started by: mmohan
1 Replies

8. UNIX for Dummies Questions & Answers

Unix History Question: Why are filenames/dirnames case sentsitive in Unix?

I tried looking for the answer online and came up with only a few semi-answers as to why file and directory names are case sensitive in Unix. Right off the bat, I'll say this doesn't bother me. But I run into tons of Windows and OpenVMS admins in my day job who go batty when they have to deal... (3 Replies)
Discussion started by: deckard
3 Replies

9. UNIX for Dummies Questions & Answers

Unix user ID's case-sensitive?

It has been quite a while since I used UNIX. I am developing a security system and I was wondering if UNIX and/or LINUX user ID's are case-sensitive. i.e. can user 'daveb' and 'Daveb' exist on the same system with completely different authorizations/priorities, etc.? (3 Replies)
Discussion started by: dmilleville
3 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question