From: David Kirkby on
On Jan 8, 6:40 pm, Stephane CHAZELAS <stephane_chaze...(a)yahoo.fr>
wrote:
> 2010-01-8, 09:38(-08), David Kirkby:
> [...]
>
> > 1) Can you tell me if the following is perfectly safe:
>
> > if [ ! -x "$SAGE_LOCAL/bin/testcc.sh" ] || [ ! -x "$SAGE_LOCAL/bin/
> > testcxx.sh" ] ; then
> >   echo "testcc.sh and/or testcxx.sh either do not exist, or are not
> > executable"
> >   exit 1
> > fi
>
> You can't get any safer than that.
>
> However note that the files may exist and not be accessible (so
> that the test above can't decide). So instead of "do not exist",
> you could say "cannot be found".

Thank you.

> Also, it's a good idea to output error messages on stderr, so:
>
> echo >&2 "..."

Cheers.
> [...]> if [ "$C_compiler" != "$C_PLUS_PLUS_compiler" ] ; then
> >     echo ""
> >     echo "ERROR: You have different C and C++ compilers."
> >     echo "ERROR  The C compiler is $C_compiler, the C++ compiler is
> > $C_PLUS_PLUS_compiler"
> [...]
> > fails to produce the expected result, as the output from the script
> > is:
> [...]
> > ERROR  The C compiler is Sun_Studio, the C++ compiler is Sun_Studio
> > ERROR: This mixture can not be used to build Sage
> > ERROR: Ensure both compilers are the same
>
> [...]
>
> Possibly $C_PLUS_PLUS_compiler has trailing spaces, or any one
> of them have invisible characters. Try piping the output of the
> script into "sed -n l"

Thank you. It was an extra space in the script being called. I'll get
that script changed. I can't work out why the script did this, but the
sed statement will go there - better to do it just once.

Thank you very much Stéphane - you have been most helpful. I was not
over impressed with that 'Advanced Bash-Scripting Guide' web page
someone pointed me at. I don't want to write for bash - I want to
write things that work with all shells (well, as many as possible).



From: bsh on
"Chris F.A. Johnson" <cfajohn...(a)gmail.com> wrote:
> bsh wrote:
> > David Kirkby <drkir...(a)gmail.com> wrote:
> > > What is the best way to compare things in shell scripts?
> > > ...

> > There is a good reason why the older syntax is deprecated,
> > and the new one added!
> > See my previous post at:
> > http://groups.google.com/group/comp.unix.shell/msg/d3076c3ca825c1e3

> There is no good reason not to use the standard syntax; it
> is not deprecated by any authority.

Chris, you didn't follow my link to read POSIX's own warning(s),
from:

http://www.opengroup.org/onlinepubs/009695399/utilities/test.html

If you had, you would have read:

APPLICATION USAGE:

Scripts should be careful when dealing with user-
supplied input that could be confused with primaries
and operators. Unless the application writer knows
all the cases that produce input to the script,
invocations like:

test "$1" -a "$2"

Should be written as:

test "$1" && test "$2"

To avoid problems if a user supplied values such as
$1 set to '!' and $2 set to the null string.
--

(I'm surprised that the above code ignores its own
recommendation to not use the implicit operator but instead
explicitly specify either "-n" or "-z").

I recommend reading this whitepaper for an in-depth discussion
of implementation detail, historical background, and problematic
syntax and cases. There are many nuances, including order-of-
precedence issues, that I was previously not aware of despite
many years of scripting experience and manpage reading -- for
instance, that "[[ ... ]]" was included in an early draft of the
POSIX standard!

=Brian
From: Chris F.A. Johnson on
On 2010-01-14, bsh wrote:
> "Chris F.A. Johnson" <cfajohn...(a)gmail.com> wrote:
>> bsh wrote:
>> > David Kirkby <drkir...(a)gmail.com> wrote:
>> > > What is the best way to compare things in shell scripts?
>> > > ...
>
>> > There is a good reason why the older syntax is deprecated,
>> > and the new one added!
>> > See my previous post at:
>> > http://groups.google.com/group/comp.unix.shell/msg/d3076c3ca825c1e3
>
>> There is no good reason not to use the standard syntax; it
>> is not deprecated by any authority.
>
> Chris, you didn't follow my link to read POSIX's own warning(s),
> from:
>
> http://www.opengroup.org/onlinepubs/009695399/utilities/test.html
>
> If you had, you would have read:
>
> APPLICATION USAGE:
>
> Scripts should be careful when dealing with user-
> supplied input that could be confused with primaries
> and operators. Unless the application writer knows
> all the cases that produce input to the script,
> invocations like:
>
> test "$1" -a "$2"
>
> Should be written as:
>
> test "$1" && test "$2"
>
> To avoid problems if a user supplied values such as
> $1 set to '!' and $2 set to the null string.
> --
>
> (I'm surprised that the above code ignores its own
> recommendation to not use the implicit operator but instead
> explicitly specify either "-n" or "-z").

That is the standard syntax I was referring to: test (a.k.a
[ ... ]) should be used instead of [[ ... ]].

> I recommend reading this whitepaper for an in-depth discussion
> of implementation detail, historical background, and problematic
> syntax and cases. There are many nuances, including order-of-
> precedence issues, that I was previously not aware of despite
> many years of scripting experience and manpage reading -- for
> instance, that "[[ ... ]]" was included in an early draft of the
> POSIX standard!

It's not there now, so there is no reason to use it.

--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====