[SAGE] Shell programming conundrum (fwd)

der.hans plug-discuss@lists.plug.phoenix.az.us
Sat, 3 Aug 2002 03:25:24 -0700 (MST)


moin, moin,

shell programming hints below :).

Actually, here's the consensual explanation for what the code questioned
below does.

First, the ':' at the beginning of the line gets the line to return a true
regardless of how the code does. This might be important for resetting $?,
but I think that wasn't the intent. In any case the rest of the line gets
evaluated.

${foo:=${foo}}

That part says that if $foo doesn't exist it should be set to $foo. Pretty
foolish as the command says to keep the current value if it doesn't exist,
but then assign the non-existant value if it doesn't exist.

There might be a need to make sure $foo exists.

${foo:=}

That does the same thing, but doesn't look ( as much ) like a typo.

The main consensus, though, is that this line should have been commented.
Suggested comments run like the following.

# blah blah depends on $foo being set. This sets $foo to null if $foo
# isn't already set

Maybe also explain why the ':' was tossed in the front.

shell is freakingly powerful :).

Comments are good. Comments explaining why something is done are excellent.
Comments explaing why something obscure was done are necessary.

ciao,

der.hans
-- 
#  https://www.LuftHans.com/
#  Magic is science unexplained. - der.hans

---------- Forwarded message ----------
Date: Fri, 2 Aug 2002 12:36:46 -0400 (EDT)
From: Adam S. Moskowitz <adamm@menlo.com>
To: SAGE Members <sage-members@usenix.org>
Subject: [SAGE] Shell programming conundrum

Normally, I consider myself an expert in shell programming, but this one
really has me stumped . . .

Consider the following excerpt of code:

	cmd1

	. . .

	cmd23

	: ${foo:=${foo}}	# <== THIS ONE!

	cmd24 $foo

	. . .

Yes, it's legal shell code. Yes, it works (for a very broad definition
of "work").

We don't know the value of "foo"; I claim it doesn't matter.

Here's the challenge: Tell me what purpose the line marked "THIS ONE!"
serves. I claim you can delete that line and the script will produce the
same exact results -- but I'd love for someone to prove me wrong.

AdamM

P.S. - No, I didn't write this; I found it in a script that someone else
       wrote that I now have to modify.