Been looking through the various shell scripts that make up Debian as a
way to learn bash & perl scripting.
So please be nice if this is a really dumb question.
Is a piece of code like the following...
while [ x"$1" != x ]; do
...
done
the same as using the -z flag for test.
It just looks like a weird way of doing things that occur frequently.
Please explain.
Lex.
--
To UNSUBSCRIBE, email to debian-dev...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org
Don't take this as gospel, but I've seen it around a lot too, particularly
in older-school shell scripts. I'm guessing it was done before the -z test
existed or something...
Andrew
It's not a dumb question, but it might be better asked on debian-users.
> Is a piece of code like the following...
>
> while [ x"$1" != x ]; do
> ...
> done
>
> the same as using the -z flag for test.
Yes.
> It just looks like a weird way of doing things that occur frequently.
The -z test is a relatively new addition (well, considering that the
original bourne shell is ~25 years old :-)), so out of habit or concern
for portability (the Sun Solaris /bin/sh doesn't support -z) people
still use the [ x"$1" != x] form.
Steve
--
Steve Greenland
The irony is that Bill Gates claims to be making a stable operating
system and Linus Torvalds claims to be trying to take over the
world. -- seen on the net
|| On Wed, Feb 04, 2004 at 05:28:27PM +1100, Lex Hider wrote:
|| > while [ x"$1" != x ]; do
|| > ...
|| > done
|| >
|| > the same as using the -z flag for test.
... not considering the negation in there.
I would write [ "${1:+x}" ], or rather even [ "${1+x}" ], distinguishing
empty and undefined.
|| > It just looks like a weird way of doing things that occur frequently.
|| Don't take this as gospel, but I've seen it around a lot too, particularly
|| in older-school shell scripts. I'm guessing it was done before the -z test
|| existed or something...
Or more likely, many people know = and !=, and know enough to implement
the test they need.
--
Vincent Zweije <zwe...@xs4all.nl> | "If you're flamed in a group you
<http://www.xs4all.nl/~zweije/> | don't read, does anybody get burnt?"
[Xhost should be taken out and shot] | -- Paul Tomblin on a.s.r.
> || > while [ x"$1" != x ]; do
> || > ...
> I would write [ "${1:+x}" ], or rather even [ "${1+x}" ], distinguishing
> empty and undefined.
Wouldn't work. Most shells (and /bin/['s) are fine with empty
arguments, but some get confused if the first character of the
argument is a dash, such that it can be interpreted as an option.
Hence the 'x' in front.
--
Henning Makholm "And why should I talk slaves' and fools' talk? I
don't want him to live for ever, and I know that he's
not going to live for ever whether I want him to or not."
Habits due to portability of the code. That could be not a major concern
in debian, but if you need to ports scripts among many flavours of
current and ancient unices, that's a common trick to avoid pitfalls
due to different versions of the test program.
--
Francesco P. Lovergine
|| Scripsit zwe...@xs4all.nl (Vincent Zweije)
|| > I would write [ "${1:+x}" ], or rather even [ "${1+x}" ], distinguishing
|| > empty and undefined.
||
|| Wouldn't work. Most shells (and /bin/['s) are fine with empty
|| arguments, but some get confused if the first character of the
|| argument is a dash, such that it can be interpreted as an option.
|| Hence the 'x' in front.
No leading dash occurs in these two examples. The substitutions yield
either "x" or "", depending on $1. But we're digressing.
The compatibility argument that was mentioned explains why avoiding -z
would be better. I don't know if there are compatibility problems when
using ${var+value}.
Ciao. Vincent.
To be more precise, the problem is the program 'test' or '[' which
is an alias for it. The shell is not the guilty in this case.
--
Francesco P. Lovergine
True enough. However, it has plenty else to attone for.
Hey, Sun, are you out there? When are you going to start shipping a
POSIX compliant OS? And no, I *don't* consider having POSIX tools over
in /usr/xpg4/bin an acceptable substitute, because it doesn't help all
the shell scripts that start with '#!/bin/sh', and work just fine on
AIX, HP-UX, and, of course, Linux.
Oh, sorry, didn't mean to go off on a rant...
Steve
--
Steve Greenland
The irony is that Bill Gates claims to be making a stable operating
system and Linus Torvalds claims to be trying to take over the
world. -- seen on the net