[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash vs. python scripts - which one is better?



Vincent Lefevre wrote:
> Bob Proulx wrote:
> > The test command was originally not a shell built-in.  It was an
> > external standalong /bin/test command.  For performance reasons it has
> 
> I don't think it is for performance reasons. Have you ever seen any
> noticeable performance gain?

I don't think there would be much performance difference but I have
not benchmarked it.  However I accept that 20 years ago it was
included for that reason and that it may have been significant then.

> 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?

Use -e if you want to test for simple file existence.  However most of
the time it is better to use -f or -r or -x or whatever you are
actually testing for.

> I think that [ has been added as a builtin to the bash shell in order
> to add features... that break compatibility!

Test was added to the shell well before bash came along.

> > But better to use the one = so that it is portable.  I prefer using an
> > underscore to hide it more but 'X' is the tradition.
> > 
> >   if [ _"$foo" = _"" ]

I was talking traditional test style here.  I break with tradition in
my scripts and use _ instead of X so that they are lower profile.

> 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.

I still prefer to use -z and -n instead.

Bob



Reply to: