BASH question: "if [ ]" and "if [[ ]]"

BASH question: "if [ ]" and "if [[ ]]"

am 29.10.2007 02:03:54 von linq936

Hi,
I am a little confused at conditional construct of BASH. I have been
using BASH for a while and I always use "if [[ ]]" as
conditional construct since its syntax is very similar to C++, I know
there is another construct, "if [ something ]", but never really check
out what is the difference.

Today I run the following command:

for i in *
do
builtin cd $i
for j in *
do
if [[ ! -f $j/lin/run/myfile ]]
then
echo $i/$j/lin/run/myfile does not exist
fi
done
builtin cd ..
done

I see it does not work, for every sub directory it says the file
does not exist, till I change the if line to:

if [ ! -f $j/lin/run/myfile ]

Then really what is the difference between the 2 constructs?

Re: BASH question: "if [ ]" and "if [[ ]]"

am 29.10.2007 11:56:29 von Yogesh Sawant

On Oct 29, 6:03 am, linq936 wrote:
> Hi,
> I am a little confused at conditional construct of BASH. I have been
> using BASH for a while and I always use "if [[ ]]" as
> conditional construct since its syntax is very similar to C++, I know
> there is another construct, "if [ something ]", but never really check
> out what is the difference.
>
> Today I run the following command:
>
> for i in *
> do
> builtin cd $i
> for j in *
> do
> if [[ ! -f $j/lin/run/myfile ]]
> then
> echo $i/$j/lin/run/myfile does not exist
> fi
> done
> builtin cd ..
> done
>
> I see it does not work, for every sub directory it says the file
> does not exist, till I change the if line to:
>
> if [ ! -f $j/lin/run/myfile ]
>
> Then really what is the difference between the 2 constructs?

According to the Advanced Bash Scripting Guide,
http://www.tldp.org/LDP/abs/html/testconstructs.html#DBLBRAC KETS

Cheers
Yogesh

Re: BASH question: "if [ ]" and "if [[ ]]"

am 29.10.2007 12:22:56 von Bill Marcum

On 2007-10-29, linq936 wrote:
> Hi,
> I am a little confused at conditional construct of BASH. I have been
> using BASH for a while and I always use "if [[ ]]" as
> conditional construct since its syntax is very similar to C++, I know
> there is another construct, "if [ something ]", but never really check
> out what is the difference.
>
> Today I run the following command:
>
> for i in *
> do
> builtin cd $i
> for j in *
> do
> if [[ ! -f $j/lin/run/myfile ]]
> then
> echo $i/$j/lin/run/myfile does not exist
> fi
> done
> builtin cd ..
> done
>
> I see it does not work, for every sub directory it says the file
> does not exist, till I change the if line to:
>
> if [ ! -f $j/lin/run/myfile ]
>
> Then really what is the difference between the 2 constructs?
>
Pathname expansion is not done inside [[ ]]. If some if the subdirectories in
the outer loop are empty, the "for j in *" loop is executed with j equal to
'*'. There is probably no file named '*/lin/run/myfile', but the wildcard is
expanded in the echo command. If you begin your script with the command
'shopt -s nullglob' the 'for i in *' and 'for j in *' loops will not be
executed when nothing matches '*'.