Bash syntax behaviour : [[ vs [


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash syntax behaviour : [[ vs [
# 1  
Old 12-25-2013
Bash syntax behaviour : [[ vs [

Hello.

In the following :
RESTORE_FF contain a file name : a_file.tar.gz
I am testing in a directory if "a_file.tar.gz" exists and or if any file like "a_file.tar.gz" exists.
So "a_file.tar.gz" will give me file exists
So "a_file.tar.gz." will give me file exists
So "a_file.tar.gz.2013_12_25" will give me file exists

Now my question :
In the search directory there is a file : a_file.tar.gz.
Why
Code:
if [[ -f $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

print nothing
and why
Code:
if [[  !  -f  $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

print :
Code:
Multiple files exists

and why
Code:
if [ -f $RESTORE_FF.* ] ; then echo "Multiple files exists" ; fi

print :
Code:
Multiple files exists

Any help is welcome
# 2  
Old 12-25-2013
Quote:
Originally Posted by jcdole
Hello.

In the following :
RESTORE_FF contain a file name : a_file.tar.gz
I am testing in a directory if "a_file.tar.gz" exists and or if any file like "a_file.tar.gz" exists.
So "a_file.tar.gz" will give me file exists
So "a_file.tar.gz." will give me file exists
So "a_file.tar.gz.2013_12_25" will give me file exists

Now my question :
In the search directory there is a file : a_file.tar.gz.
Why
Code:
if [[ -f $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

print nothing
and why
Code:
if [[  !  -f  $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

print :
Code:
Multiple files exists

and why
Code:
if [ -f $RESTORE_FF.* ] ; then echo "Multiple files exists" ; fi

print :
Code:
Multiple files exists

Any help is welcome

Code:
$ cat test
#!/bin/bash

# Create file and compress for testing..
touch test
tar -zcvf a_file.tar.gz test 

# List file
ls *.gz 

# Variable declaration
RESTORE_FF="a_file.tar.gz"

# -f : Return true value if file exists and regular file

# Double bracket, if file exist print message
if [[ -f $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

# Double bracket with not operator, if file not exist print message
if [[  !  -f  $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

# Single bracket, if file exist print message
if [ -f $RESTORE_FF.* ] ; then echo "Multiple files exists" ; fi

Code:
$ bash test 
test
a_file.tar.gz
Multiple files exists

$ sh test 
test
a_file.tar.gz
test: 16: test: [[: not found
test: 19: test: [[: not found

if you want to check multiple files, with wildcard you can use ls orfind something like this
Code:
if ls  path/to/files*  &> /dev/null; then
    echo "files exist"
fi

# 3  
Old 12-25-2013
Perhaps this may be enough:

Code:
exists() {
  [ -e "$1" ]
}

Code:
if exists "$RESTORE_FF"*; then
  echo "At least one file exists"
fi

If you need to specifically test for a regular file, you could try something like
Code:
exists_file() {
  for _f do
    [ -f "$_f" ] && return
  done
  return 1
}

# 4  
Old 12-25-2013
I thank every body for their solutions, but my question was about the syntax and why I got these three behavior :

In the search directory there is a file : a_file.tar.gz.
Why

Code:
if [[ -f $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

print nothing
and why

Code:
if [[  !  -f  $RESTORE_FF.* ]] ; then echo "Multiple files exists" ; fi

print :
Code:
Multiple files exists

and why

Code:
if [ -f $RESTORE_FF.* ] ; then echo "Multiple files exists" ; fi

print :

Code:
Multiple files exists

# 5  
Old 12-25-2013
In the case of single brackets, pathname expansion does get performed, however in the case of double brackets this is not the case, so in your example with single brackets after variable expansion of $RESTORE_FF.* to a_file.tar.gz.*, the pathname pattern gets expanded to a_file.tar.gz., so it would look for the literal file a_file.tar.gz. and in the case of double brackets for the literal file a_file.tar.gz.*, which does not exist. The example with single brackets will give a syntax error if more than one file with the given pattern exists.

Last edited by Scrutinizer; 12-25-2013 at 07:15 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 12-26-2013
[SOLVED] Bash syntax behaviour : [[ vs [

Quote:
Originally Posted by Scrutinizer
In the case of single brackets, pathname expansion does get performed, however in the case of double brackets this is not the case, so in your example with single brackets after variable expansion of $RESTORE_FF.* to a_file.tar.gz.*, the pathname pattern gets expanded to a_file.tar.gz., so it would look for the literal file a_file.tar.gz. and in the case of double brackets for the literal file a_file.tar.gz.*, which does not exist. The example with single brackets will give a syntax error if more than one file with the given pattern exists.
Thank you very much.

Last edited by jcdole; 12-26-2013 at 04:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error in subtraction in Bash

I am sharing a code snippet. for (( i=0; i<=$(( $count -1 )); i++ )) do first=${barr2} search=${barr1} echo $first echo "loop begins" for (( j=0; j<=5000; j++ )) do if } == $search ]]; then echo $j break; fi done second=${harr2} echo $second (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Bash syntax problem

Hello! i try to understand the art of bash scripting but unfortunately, more i try and less i understand it. Can someone tell me how i can learn its logic? i will give you an example why its making me crazy. Look at this basic script: my for loops are working like this, but it took me more than... (10 Replies)
Discussion started by: sablista
10 Replies

3. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

4. Shell Programming and Scripting

Bash syntax

Hello, I have seen this syntax, { ;;};quite often and I don't know what it means exactly. It seems like a distinctive thing of Bash, so it's been used for the logo of the last bug, ShellShock: All you need to know about the Bash Bug vulnerability | Symantec Connect I have also seen... (3 Replies)
Discussion started by: Kibou
3 Replies

5. Shell Programming and Scripting

Bash syntax error

while read line do mkdir $line scp -r Docking_results/docking_$line.pdb $line/ cd /$line/ set a=`grep ENDMDL docking_'$line'.pdb | wc -l` set b=`expr $a - 2` csplit -k -s -n 3 -f docking_'$line'. docking'$line'.pdb '/^ENDMDL/+1' '{'$b'}' foreach f (... (4 Replies)
Discussion started by: chrisjorg
4 Replies

6. Red Hat

Device-mapper behaviour booting on init=bin/bash

Good morning Recently we needed to change the password from a redhat 6.5 system that no one knew the root password. Starting the system with the init=/bin/bash method took us to the following scenario: system_vg active with only root_lv and tmpfs mounted. our entries at fstab are like... (1 Reply)
Discussion started by: Ikaro0
1 Replies

7. Web Development

Bash Script, ec2addtag syntax?

ec2addtag --region us-west-1 vol1234 --tag Name=$nameinst; It should execute ec2addtag --region us-west-1 vol1234 --tag Name=webserver; Instead it thinks that Name is equal to that variable. Please help. Thanks! Please use code tags! (0 Replies)
Discussion started by: svalenciatech
0 Replies

8. Shell Programming and Scripting

Using pipe in bash - is this the expected behaviour?

Hi all, Am trying to convert a script from ksh to bash :wall:. One of the sub is something like below: #!/bin/bash declare -a array01 step_01_test() { local count=0 ps -ef | grep watch | grep -v grep | awk '{ print $8 }' | while read line do let count=${count}+1 ... (1 Reply)
Discussion started by: newbie_01
1 Replies

9. Shell Programming and Scripting

Solaris bash syntax different from Linux?

I have a script that's meant to check the disk usage on a particular volume and delete the oldest logfile if it's over a certain percentage. It runs fine on a Linux machine, but on a Solaris one, I get this error: diskspace_check.sh: syntax error at line 3: `diskspace=$' unexpected I assume... (2 Replies)
Discussion started by: cara_k
2 Replies

10. Shell Programming and Scripting

BASH Script syntax error

I'm trying to write a simple script that takes all the .tar.gz files in a directory and verifies them by using the gzip -tv command: for zip in *.tar.gz do gzip -tv $zip if ; then #Check return code from tar echo "File ${zip} verified OK." exit... (4 Replies)
Discussion started by: kelldan
4 Replies
Login or Register to Ask a Question