Re: bash vs. python scripts - which one is better?
On 2007-08-21 17:50:53 -0600, Bob Proulx wrote:
> Vincent Lefevre wrote:
> > vin:~> bash
> > vlefevre@vin:~$ touch exists
> > vlefevre@vin:~$ [ ! -a exists ] || echo found
> > vlefevre@vin:~$ /usr/bin/[ ! -a exists ] || echo found
> > found
>
> You are running afoul of -a ambiguity. Stop that. Do you mean AND
> such as -a intends?
I didn't mean any specific behavior. I just noted that /usr/bin/[
and [ bash builtin are not compatible.
> Use -e if you want to test for simple file existence.
Of course, but I also have to live with scripts (not written by me)
that use '-a' (and of course, I cannot really in advance if '-a' is
used or not). That's the real world. :(
Now, what about the following test, which does not test file existence?
vlefevre@vin:~$ [ true -a \( ! -a \) ] && echo OK
bash: [: `)' expected, found ]
vlefevre@vin:~$ /usr/bin/[ true -a \( ! -a \) ] && echo OK
vlefevre@vin:~$
This is unspecified by POSIX, but test(1) has its own documentation.
If you prefer, the bash builtin breaks the compatibility with test(1)
even if it is non-standard.
> > Are you sure that
> >
> > [ "$foo" = "" ]
> >
> > isn't POSIX sh? IMHO, it is perfectly valid (note: the quotes are
> > important).
>
> POSIX defines this okay but as I was noting the traditional use was to
> use the X to prevent the old test from parsing it as an option. In
> the traditional environment this was not protected by the new POSIX
> requirement to count the number of program arguments provided.
OK, I now understand. I read somewhere else that the cause was empty
strings. Even in a POSIX environment, there's still a problem if $foo
is '!' (if I'm not mistaken, this is an "unspecified results" case).
> I still prefer to use -z and -n instead.
Yes.
--
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)
Reply to: