Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple shell scripting question from a newbie.

0 views
Skip to first unread message

Lex Hider

unread,
Feb 4, 2004, 1:30:21 AM2/4/04
to
Hi,

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

Andrew Pollock

unread,
Feb 4, 2004, 8:30:18 AM2/4/04
to
On Wed, Feb 04, 2004 at 05:28:27PM +1100, Lex Hider wrote:
> Hi,
>
> 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.

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

Steve Greenland

unread,
Feb 4, 2004, 8:40:18 AM2/4/04
to
On 04-Feb-04, 00:28 (CST), Lex Hider <alex...@pacific.net.au> wrote:
> So please be nice if this is a really dumb question.

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

Vincent Zweije

unread,
Feb 4, 2004, 8:50:20 AM2/4/04
to
On Wed, Feb 04, 2004 at 10:44:24PM +1000, Andrew Pollock wrote:

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

signature.asc

Henning Makholm

unread,
Feb 4, 2004, 9:50:11 AM2/4/04
to
Scripsit zwe...@xs4all.nl (Vincent Zweije)

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

Francesco P. Lovergine

unread,
Feb 4, 2004, 9:50:18 AM2/4/04
to
On Wed, Feb 04, 2004 at 05:28:27PM +1100, Lex Hider wrote:
>
> 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.
>

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

Vincent Zweije

unread,
Feb 4, 2004, 1:10:21 PM2/4/04
to
Henning:

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

signature.asc

Francesco P. Lovergine

unread,
Feb 6, 2004, 10:10:10 AM2/6/04
to
On Wed, Feb 04, 2004 at 07:13:24AM -0600, Steve Greenland wrote:
> for portability (the Sun Solaris /bin/sh doesn't support -z) people

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

Steve Greenland

unread,
Feb 6, 2004, 7:40:05 PM2/6/04
to
On 06-Feb-04, 08:50 (CST), "Francesco P. Lovergine" <fra...@debian.org> wrote:
> On Wed, Feb 04, 2004 at 07:13:24AM -0600, Steve Greenland wrote:
> > for portability (the Sun Solaris /bin/sh doesn't support -z) people
>
> 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.

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

0 new messages