From: Dave on
I have a shell script written by someone else, which starts


#!/usr/bin/env bash


then has the line:

unset DISTUTILS_DEBUG

It's a bit pointless, as DISTUTILS_DEBUG is not set in the script, but there is
some code commented out, where is was obviously set at one time.

I've added

set -e

in this script, and find the script is exiting. Putting a few echo statements
in, I see:

echo "about to unset DISTUTILS_DEBUG"
unset DISTUTILS_DEBUG
echo "unset DISTUTILS_DEBUG"

The "about to unset DISTUTILS_DEBUG"

is printed, but the

unset DISTUTILS_DEBUG

is not, so I assume it's the fact one tries to unset something that is not set
is what's causing the problem.

Is this behavior to be expected? It does not happen under OS X, Linux or
Solaris, but is causing a problem on HP-UX 11.11.

If it's not ok to do this, how can I test if the variable is set, before trying
to unset it? I know how to test if a variable is of zero length (use -z in
test), but that is not quite the same as a variable being set or not.



Dave


--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
From: Icarus Sparry on
On Mon, 14 Dec 2009 17:05:52 +0000, Dave wrote:

> I have a shell script written by someone else, which starts
>
>
> #!/usr/bin/env bash
>
>
> then has the line:
>
> unset DISTUTILS_DEBUG
>
> It's a bit pointless, as DISTUTILS_DEBUG is not set in the script, but
> there is some code commented out, where is was obviously set at one
> time.
>
> I've added
>
> set -e

set -x

would probably be more useful.

> in this script, and find the script is exiting. Putting a few echo
> statements in, I see:
>
> echo "about to unset DISTUTILS_DEBUG"
> unset DISTUTILS_DEBUG
> echo "unset DISTUTILS_DEBUG"
>
> The "about to unset DISTUTILS_DEBUG"
>
> is printed, but the
>
> unset DISTUTILS_DEBUG
>
> is not, so I assume it's the fact one tries to unset something that is
> not set is what's causing the problem.
>
> Is this behavior to be expected? It does not happen under OS X, Linux or
> Solaris, but is causing a problem on HP-UX 11.11.
>
> If it's not ok to do this, how can I test if the variable is set, before
> trying to unset it? I know how to test if a variable is of zero length
> (use -z in test), but that is not quite the same as a variable being set
> or not.

The normal way is to use the
${DISTUTILS_DEBUG+yes}
variable expansion. This will evaluate to "yes" if the variable is set,
even if it is the zero length string, and nothing otherwise.

So hopefully
test -z "${DISUTILE_DEBUG+yes} && unset DISTUTILS_DEBUG
will fix your problem. What version of bash are you running on your HP-UX
machine?

From: Dave on
Dave wrote:
> I have a shell script written by someone else, which starts
>
>
> #!/usr/bin/env bash
>
>
> then has the line:
>
> unset DISTUTILS_DEBUG
>
> It's a bit pointless, as DISTUTILS_DEBUG is not set in the script, but
> there is some code commented out, where is was obviously set at one time.
>
> I've added
>
> set -e
>
> in this script, and find the script is exiting. Putting a few echo
> statements in, I see:
>
> echo "about to unset DISTUTILS_DEBUG"
> unset DISTUTILS_DEBUG
> echo "unset DISTUTILS_DEBUG"
>
> The "about to unset DISTUTILS_DEBUG"
>
> is printed, but the
>
> unset DISTUTILS_DEBUG
>
> is not, so I assume it's the fact one tries to unset something that is
> not set is what's causing the problem.
>
> Is this behavior to be expected? It does not happen under OS X, Linux or
> Solaris, but is causing a problem on HP-UX 11.11.
>
> If it's not ok to do this, how can I test if the variable is set, before
> trying to unset it? I know how to test if a variable is of zero length
> (use -z in test), but that is not quite the same as a variable being set
> or not.
>
>
>
> Dave
>
>

Looking at POSIX 2004,

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

it says:

"Unsetting a variable or function that was not previously set shall not be
considered an error and does not cause the shell to abort."

so it looks like this is a place where HP-UX is not POSIX 2004 compatible.

Dave


--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.
From: Stachu 'Dozzie' K. on
On 14.12.2009, Dave wrote:
> I have a shell script written by someone else, which starts
>
>
> #!/usr/bin/env bash
>
>
> then has the line:
>
> unset DISTUTILS_DEBUG
>
> It's a bit pointless, as DISTUTILS_DEBUG is not set in the script,

Not really pointless. Author doesn't know in what environment his script
will be run. This unset ensures the script will have DISTUTILS_DEBUG
unset.
Personally I prefer to set variables in such cases to empty string.

> but there is
> some code commented out, where is was obviously set at one time.
>
> I've added
>
> set -e
>
> in this script, and find the script is exiting. Putting a few echo statements
> in, I see:
>
> echo "about to unset DISTUTILS_DEBUG"
> unset DISTUTILS_DEBUG
> echo "unset DISTUTILS_DEBUG"
>
> The "about to unset DISTUTILS_DEBUG"
>
> is printed, but the
>
> unset DISTUTILS_DEBUG
>
> is not, so I assume it's the fact one tries to unset something that is not set
> is what's causing the problem.
>
> Is this behavior to be expected?

It is not expected.
http://www.opengroup.org/onlinepubs/009695399/utilities/unset.html
#v+
Unsetting a variable or function that was not previously set shall not
be considered an error and does not cause the shell to abort.
#v-

Maybe DISTUTILS_DEBUG is set read-only somewhere?

> It does not happen under OS X, Linux or
> Solaris, but is causing a problem on HP-UX 11.11.
>
> If it's not ok to do this, how can I test if the variable is set, before trying
> to unset it?

Just set it to anything (empty string will be OK, I think) and then
unset. And leave the comment what the hell is such a strange code piece
for.

> I know how to test if a variable is of zero length (use -z in
> test), but that is not quite the same as a variable being set or not.

--
Stanislaw Klekot
From: Dave on
Icarus Sparry wrote:
> On Mon, 14 Dec 2009 17:05:52 +0000, Dave wrote:
>
>> I have a shell script written by someone else, which starts
>>
>>
>> #!/usr/bin/env bash
>>
>>
>> then has the line:
>>
>> unset DISTUTILS_DEBUG
>>
>> It's a bit pointless, as DISTUTILS_DEBUG is not set in the script, but
>> there is some code commented out, where is was obviously set at one
>> time.
>>
>> I've added
>>
>> set -e
>
> set -x
>
> would probably be more useful.
>
>> in this script, and find the script is exiting. Putting a few echo
>> statements in, I see:
>>
>> echo "about to unset DISTUTILS_DEBUG"
>> unset DISTUTILS_DEBUG
>> echo "unset DISTUTILS_DEBUG"
>>
>> The "about to unset DISTUTILS_DEBUG"
>>
>> is printed, but the
>>
>> unset DISTUTILS_DEBUG
>>
>> is not, so I assume it's the fact one tries to unset something that is
>> not set is what's causing the problem.
>>
>> Is this behavior to be expected? It does not happen under OS X, Linux or
>> Solaris, but is causing a problem on HP-UX 11.11.
>>
>> If it's not ok to do this, how can I test if the variable is set, before
>> trying to unset it? I know how to test if a variable is of zero length
>> (use -z in test), but that is not quite the same as a variable being set
>> or not.
>
> The normal way is to use the
> ${DISTUTILS_DEBUG+yes}
> variable expansion. This will evaluate to "yes" if the variable is set,
> even if it is the zero length string, and nothing otherwise.
>
> So hopefully
> test -z "${DISUTILE_DEBUG+yes} && unset DISTUTILS_DEBUG
> will fix your problem. What version of bash are you running on your HP-UX
> machine?
>

bash-2.04$ bash --version
GNU bash, version 2.04.0(1)-release (hppa2.0w-hp-hpux11.00)
Copyright 1999 Free Software Foundation, Inc.
bash-2.04$

Is this likely to be a problem with HP-UX, or bash? The bash command is

/opt/OpenSource/bin/bash

so from the location, one might guess HP do not support 'bash'

I appreciate a newer version of bash might not suffer this problem, but I'd
rather sort out issues which occur with old versions of commands too - not
everyone runs the latest. The code I am using is checked by quite a few people
on the more common linux and OS X distributions, so most people will not find
issues like this.


--
I respectfully request that this message is not archived by companies as
unscrupulous as 'Experts Exchange' . In case you are unaware,
'Experts Exchange' take questions posted on the web and try to find
idiots stupid enough to pay for the answers, which were posted freely
by others. They are leeches.