It is currently Tue May 22, 2018 1:48 pm

File exists test problem

All times are UTC - 6 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Sep 15, 2009 4:05 am   

Joined: Tue Sep 15, 2009 3:51 am
Posts: 1
Greetings all!

Ok I'm pretty new at Bash scripting, but like a nerdjunkie I'm hooked already ;)
However I'm having a strange problem when checking if a file exists, here's some output to explain:

bash-3.00$ echo $dtstamp
bash-3.00$ touch SAPHREPI20090810122309.CSV
bash-3.00$ if [[ -e *$dtstamp??????.CSV ]]; then  echo "CSV file exists, removing"; fi
bash-3.00$ ls *$dtstamp??????.CSV

As you can see, the if [[ -e ... ]] can't seem to find the file, however if I ls using the same variables it can! Can anyone explain why this might be happening? Any suggestions would be greatly appreciated!


I have now figured out that the cause seems to be using [[ ]] instead of just []. The reason I used double-bracketing was to avoid having to quote a variable containing a directory path with a space character in it in the full version of the script:
if [[ -e $archivepath/*$dtstamp??????.CSV ]];
instead of
if [ -e "$archivepath"/*$dtstamp??????.CSV ];

I have reverted to the second method now and that seems to work, but could someone point me in the direction of some more information about the effects of double-bracketing so I can understand why this was happening, please?

 PostPosted: Tue Sep 15, 2009 2:22 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Taken from the Bash Beginner's Guide:

[] vs. [[]]

Contrary to [, [[ prevents word splitting of variable values. So, if VAR="var with spaces", you do not need to double quote $VAR in a test - even though using quotes remains a good habit. Also, [[ prevents pathname expansion, so literal strings with wildcards do not try to expand to file names. Using [[, == and != interpret strings to the right as shell glob patterns to be matched against the value to the left, for instance: [[ "value" == val* ]].

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 8 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group