Discussion:
test usage in OpenBSD shell scripts
Stephen Marley
2005-03-05 15:46:22 UTC
Permalink
Can someone remind me why OpenBSD's shell scripts (/etc/rc for example)
use test constructs like this:

[ "X${pf}" != X"NO" ]

why is the X used? Isn't this equivalent and less obfuscated?:

[ "$pf" != NO ]

Cheers.
--
***@cl-is.com
Stephen Marley
2005-03-05 16:02:41 UTC
Permalink
Post by Stephen Marley
Can someone remind me why OpenBSD's shell scripts (/etc/rc for example)
[ "X${pf}" != X"NO" ]
[ "$pf" != NO ]
If the variable NO does not exist, the first example will still work whereas
the second fails.
NO is a string. If $pf doesn't exist the second case reduces to

[ "" != NO ] which is still logically equivalent to [ "X" != X"NO" ]

To my thinking, as long as the variable is protected by "" then the test
will not have a syntax error.

I think there's some historical reason for it, but I can't remember.

Cheers.
--
***@cl-is.com
Jet Nul
2005-03-05 16:48:49 UTC
Permalink
http://www.ooblick.com/text/sh/ at the end:

|| Potentially unset variables
|| Remember that variables may not be set, or may be set to the null
|| string. For instance, you may be tempted to write
||
|| if [ $answer = yes ]; then
||
|| However, $answer might be set to the empty string, so sh would see
|| if [ = yes ]; then, which would cause an error. Better to write
||
|| if [ "$answer" = yes ]; then
||
|| The danger here is that $answer might be set to -f, so sh would see
|| if [ -f = yes ]; then, which would also cause an error.
||
|| Therefore, write
||
|| if [ x"$answer" = xyes ]; then
||
|| which avoids both of these problems.
Han Boetes
2005-03-05 17:10:32 UTC
Permalink
Post by Jet Nul
|| Potentially unset variables
|| Remember that variables may not be set, or may be set to the null
|| string. For instance, you may be tempted to write
||
|| if [ $answer = yes ]; then
||
|| However, $answer might be set to the empty string, so sh would see
|| if [ = yes ]; then, which would cause an error. Better to write
||
|| if [ "$answer" = yes ]; then
||
|| The danger here is that $answer might be set to -f, so sh would see
|| if [ -f = yes ]; then, which would also cause an error.
||
|| Therefore, write
||
|| if [ x"$answer" = xyes ]; then
||
|| which avoids both of these problems.
This is a bug in very old sh implementations, but not in pdksh as
is included with OpenBSD.

[ "$answer" = yes ]

Will work fine with ksh. But the OpenBSD teams wants to maintain
portability with older shells, just incase someone decides he
wants to install such an old shell.




# Han
Stephen Marley
2005-03-05 17:55:04 UTC
Permalink
Post by Han Boetes
This is a bug in very old sh implementations, but not in pdksh as
is included with OpenBSD.
[ "$answer" = yes ]
Will work fine with ksh. But the OpenBSD teams wants to maintain
portability with older shells, just incase someone decides he
wants to install such an old shell.
You're right [ -f = yes ] doesn't cause a syntax error in zsh either (my
choice of interactive shell). I've never used the X"$var" trick in my
own scripts (always /bin/sh), but I suppose it's still a good idea if
portability is a concern.
--
***@cl-is.com
Han Boetes
2005-03-05 18:17:49 UTC
Permalink
Post by Stephen Marley
You're right [ -f = yes ] doesn't cause a syntax error in zsh
either (my choice of interactive shell). I've never used the
X"$var" trick in my own scripts (always /bin/sh), but I suppose
it's still a good idea if portability is a concern.
Take a look at any GNU configure script.

That's supposed to be portable. Amazing stuff you find in those
scripts :-)



# Han
Jet Nul
2005-03-05 18:17:27 UTC
Permalink
This post might be inappropriate. Click to display it.
Stephen Marley
2005-03-05 18:56:50 UTC
Permalink
Post by Jet Nul
zsh for interactive, eh?
Probably the most powerful shell in the world. It's been described as
ksh on steroids. If you're a csh junkie, there are options to ease your
transition. I've been using it since the early 90's. Blows all other
shells away for interactive use IMO. More info at
http://www.adamspiers.org/computing/zsh/ and http://zsh.sunsite.dk/
--
***@cl-is.com
Jet Nul
2005-03-05 21:22:26 UTC
Permalink
Copy. I'll look into it. Back in the 90's I noticed the elite
admins in Computing Services used zsh, funny I never
gave it a serious try before. Thanks for the pointers.


On Sat, 5 Mar 2005 18:56:50 +0000, Stephen Marley
Post by Stephen Marley
Post by Jet Nul
zsh for interactive, eh?
Probably the most powerful shell in the world. It's been described as
ksh on steroids. If you're a csh junkie, there are options to ease your
transition. I've been using it since the early 90's. Blows all other
shells away for interactive use IMO. More info at
http://www.adamspiers.org/computing/zsh/ and http://zsh.sunsite.dk/
--
Stephen Marley
2005-03-05 17:13:00 UTC
Permalink
Post by Jet Nul
|| Potentially unset variables
|| Remember that variables may not be set, or may be set to the null
|| string. For instance, you may be tempted to write
||
|| if [ $answer = yes ]; then
||
|| However, $answer might be set to the empty string, so sh would see
|| if [ = yes ]; then, which would cause an error. Better to write
||
|| if [ "$answer" = yes ]; then
||
|| The danger here is that $answer might be set to -f, so sh would see
|| if [ -f = yes ]; then, which would also cause an error.
||
|| Therefore, write
||
|| if [ x"$answer" = xyes ]; then
||
|| which avoids both of these problems.
That's it. Defensive scripting. Thanks!
--
***@cl-is.com
Loading...