Multiple conditions in a CASE statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple conditions in a CASE statement
# 15  
Old 03-17-2011
  • In glob, ? matches any single character, and in regex . does the same.
  • In glob, * matches any 0-max characters, and in reges .* doees that.
  • In glob you can match any character in a list, with ranges, like characters legal for C variable names: [a-zA-Z0-9_] Regex does the same, the same way but can also do negative lists, like not a number: [^0-9].
  • In regex you have * for any count, but not in glob.
  • Extended regex, like egrep or grep -E, has + for count 0 or one, \{##\} for specific count, and \{##-##\} for ranges of count.
  • Extended regex has parentheses and OR operator | for lists of alternatives, and glob in case does the same: From (Tom|Dick|Harry).
  • Extended regex has morphed in ways that make old scripts break, like for beginning of word \< disappeared and \b came in its place for the position at the edge of a word. One friend speculates that the PERL lovers had their way over traditional UNIX thought.
  • Apparently bash has extended globbing available, too. As I recall, the context can be switched between regex and glob with different wrapping operators in if/while tests.
# 16  
Old 03-17-2011
So when would you glob and when would you use regular expressions? I'm still somewhat confused.Smilie
# 17  
Old 03-17-2011
Quote:
Originally Posted by Straitsfan
So when would you glob and when would you use regular expressions? I'm still somewhat confused.Smilie
Most of the time you won't have the option to choose between using glob or regular expressions, and will have to use what the tool offers. As a general rule of thumb you will be using glob for the shell prompt, and regex for everything else.

For example if from a shell prompt you type ls p* you would expect the shell to expand p* to any file begining with p (glob) not any file with zero or more p's anywhere in the name (regex). Which highlights another difference in glob (patterns span the whole string, like having an implied "^" at the front and "$" at the end with regex).

Here is a little table with glob and regex equivalent

globregex
b^b$
b*^b.*$
*.jpg^.*\.jpg$
[ab]*^[ab].*$

Last edited by Chubler_XL; 03-17-2011 at 08:19 PM..
# 18  
Old 03-17-2011
I think I get it -- glob is used for finding filenames and regex is used for matching strings (as in a piece of text)?
# 19  
Old 03-17-2011
More information.
Most things can be found on the command line with 'man'. Try 'man bash' and then search for Pattern Matching with '/Pattern Matching'. Also try 'man grep' and read through the section about REGULAR EXPRESSIONS.

I did a search of the output from 'man bash' with '/regex' and it mentioned the regular expression matching operator '=~', also used in PERL. It says 'matched accordingly (as in regex(3))'. This means I can look up the information with 'man 3 regex' assuming I have the man pages installed from Linux Documentation Project.
# 20  
Old 03-17-2011
Glob patterns can (and usually do) match against filenames. But glob patterns in case options and shell if expressions are tested against strings not filenames eg:

Code:
$ [[ "salad" == s* ]] && echo yes
yes
$ [[ "salad" == s.* ]] && echo yes
$ [[ "s.jpg" == s.* ]] && echo yes
yes

# 21  
Old 03-17-2011
Many languages allow you to use both glob and regex to match strings. PERL, TCL, and Java are a few examples.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

3. Shell Programming and Scripting

Multiple conditions inside my if statement

Hello, I am using shell scripting and I am recieving odd results from my if statement if I want it to enter the loop only if L1 is equal to zero and one of the other criteria are filled, however it is entering at other times as well. What can i do to fix this? i tried seperating it... (6 Replies)
Discussion started by: ryddner
6 Replies

4. Shell Programming and Scripting

Choose multiple conditions in case?

Hi all, I am attempting to create a shell script to optimize some routine process. I want this script can let user select the actions they want to do. But I met a problem that my script can only read one input and then do one action. Is it possible to let my script to run more than one... (2 Replies)
Discussion started by: kaiya
2 Replies

5. UNIX for Dummies Questions & Answers

Multiple Find Conditions in IF Statement - UNIX

When I try the below if Condition with single condition its working fine. But when I try to Club both its working . But giving wrong results. In my case cond1 = -f ${filename1} = true cond2 = -f ${filename2} = true But Cond1 & Cond2 is resulting in False ??? Please advise ... (5 Replies)
Discussion started by: Shiny_Reddy
5 Replies

6. Shell Programming and Scripting

Multiple Conditions Perl if Statement

Hello, I'm trying to put together a script that involves pulling data from a config file. I'm attempting to write an if statement to validate one of the pieces of data from the config file, but I think I'm fat fingering it somehow. $config{VALUE} is being pulled from a config file but can only... (4 Replies)
Discussion started by: Picch
4 Replies

7. Shell Programming and Scripting

multiple lines inside a case statement

echo "please enter ur choice.. 1. Make a file. 2. Display contents 3. Copy the file 4. Rename the file 5. Delete the file 6. Exit" read choice case $choice in 1 ) echo enter the file name read fname if then echo... (2 Replies)
Discussion started by: gotam
2 Replies

8. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

9. Shell Programming and Scripting

if statement with two conditions

Hi, I am trying to use && set up to match two conditions within ksh: if && then '''Do something if somehow, I keep getting error message telling me that ] is missing. What's wrong with my code? Thanks a lot for your help! (1 Reply)
Discussion started by: cin2000
1 Replies
Login or Register to Ask a Question