bash with: if, elif & regex not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash with: if, elif & regex not working
# 1  
Old 01-25-2010
bash with: if, elif & regex not working

Why is only hello3 being printed? There must be some kind of syntax problem because the file list definitely includes all the file extensions line by line.

PHP Code:
#!/bin/bash

find '/home/myuser/folder/' -name '*.c' -type f | while read F
do
    if [[ 
"$F== '*.txt.c' ]] # if the file name ends in .txt.c
    
then
        
echo hello1
    elif 
[[ "$F== '*.c' && "$F!= '*.txt.c' && "$F!= '*.xml.c' ]] # if the file name does end in .c but not in txt.c nor .xml.c
    
then
        
echo hello2
    
else # is this else necessary ?
        
echo hello3
    fi
done 
# 2  
Old 01-25-2010
Try:
Code:
    if [[ $F == *.txt.c ]] # if the file name ends in .txt.c
    then
        echo hello1
    elif [[ $F == *.c && $F != *.txt.c && $F != *.xml.c ]] # if the file name ...
    then

# 3  
Old 01-25-2010
If I was doing the test you are trying to do with:
Quote:
if [[ "$F" == '*.txt.c' ]]
which is testing for a file literally called *.txt.c, if you changed the test to == "*.txt.c" then you would be comparing $F with a list of files in the pwd named <anything>.txt.c.

I would do it as:
Code:
if [[ -n "`echo ${F} | grep '\.txt\.c$'`" ]]

But then I tend to write Bourne shell with a few Korn shell enhancements when necessary.[COLOR="#738fbf"]

Last edited by TonyFullerMalv; 01-25-2010 at 07:22 PM.. Reason: Corrected code now edit button has reappeared!
# 4  
Old 01-25-2010
Inside [[ ]] word splitting and wildcard expansion are not performed. So if you use == "*.txt.c" you are still comparing to a literal asterisk. Therefore you need to drop the "" as well in the case of pattern. "" are used for literal string matches. So you can use for example:
Code:
[[ $var == pattern ]] || [[ $var == "string" ]]

# 5  
Old 01-25-2010
OK, I just realized that my system uses dash (not bash). So it works with bash but in dash it ouputs these errors. Any idea why? I really want to stick with dash syntax and not use programs like grep etc. when not strictly necessary.

This works in bash but not in dash:
PHP Code:
#!/bin/dash

find '/home/myuser/folder/' -name '*.c' -type f | while read F
do
    if [[ 
$F == *.css.]]
    
then
        
echo hello1
    elif 
[[ $F == *.&& $F != *.txt.&& $F != *.xml.]]
        echo 
hello2
    
else
        echo 
hello3
    fi
done 

Quote:
./shell.sh: 14: [[: not found
./shell.sh: 14: [[: not found
hello3
./shell.sh: 14: [[: not found
./shell.sh: 14: [[: not found
hello3
# 6  
Old 01-25-2010
Bourne shell only accepts:
Code:
if [ test ]

Perhaps dash is the same?
# 7  
Old 01-25-2010
Dash is posix compliant without all the additional bells and whistles that bash provides, so the [[ ... ]] is not available. You can use the [ ... ] construct, but not with patterns. So I think you should use a case statement:

Code:
find '/home/myuser/folder/' -name '*.c' -type f |
while read F
do
  case $F in
    *.css.c)         echo hello1 ;;
    *.txt.c|*.xml.c) echo hello3 ;;
    *.c)             echo hallo2 ;;
    *)               echo hello3 ;;
  esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to make working this regex in perl?

Hello to all, The Regex below is supposed to match all strings except RR45. I've tested in regex101.com and it works, butwhen I try to use it with the perl command below I get the error shown. Regex=(?<=^|RR45)(?!RR45).+?(?=RR45|$) How to fix this? I'm using Cygwin. $ echo... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

2. Shell Programming and Scripting

Regex not working

I am using a regex to exactly match a string abcdef as ^abcdef$. But it does'nt seem to work :( (11 Replies)
Discussion started by: gaurav99
11 Replies

3. Shell Programming and Scripting

If statement with [[ ]] and regex not working as expected

Using BASH: $ if -- ::00" ]]; then echo "true"; else echo "false"; fi false Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. UNIX for Dummies Questions & Answers

Gsub regex not working

I have a number of files that I pass through awk/gsub. I believe to have found a working regex and on 'test bed' sites it matches, however within gsub it does not. Examples: Initial data: /Volumes/Daniel/Public/Drop Box/_Hellsing_Ultimate_OVA_-_10_.mkv gsub & regex: gsub("\]+\]","" ... (4 Replies)
Discussion started by: unknownn
4 Replies

5. Shell Programming and Scripting

[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing. What I have understood is : -- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found -- branch B="$B $C" is to install the software --... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

7. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

8. Shell Programming and Scripting

BASH regex (convert from working perl version)

Hi there, I need to test that a variable ($VAR) matches a regex mask in BASH. I have the exact thing working in perl (below), but could somebody advise me how i would do the same in BASH ? do i need to use something like egrep ? #!/bin/perl -w my $VAR = "some value"; if ( $VAR =~... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

regex to remove text before&&after comma chars

Hi, all: I have a question about "cleaning up" a huge file with regular expression(s) and sed: The init file goes like this: block1,blah-blah-blah-blah,numseries1,numseries2,numseries3,numseries4 block2,blah-blah-blah-blah-blah,numseries,numseries2,numseries3,numseries4 ...... (3 Replies)
Discussion started by: yomaya
3 Replies

10. Shell Programming and Scripting

gnu sed regex grouping not working?

Hello, from the gnu sed manual, I should be able to do this: `\(REGEXP\)' Groups the inner REGEXP as a whole, this is used to: * Apply postfix operators, like `\(abcd\)*': this will search for zero or more whole sequences of `abcd', while `abcd*' ... (3 Replies)
Discussion started by: Allasso
3 Replies
Login or Register to Ask a Question