check for existence of environment variable

  • Follow


hi all,

I am having a problem checking for the existence of an environment
variable on an new HP-UX box (11.11) under ksh: This script for
example:

if [ -n "$MYVAR" ] ; then
        echo "MYVAR is set"
else
        echo "MYVAR is not set"
fi

runs under ksh on an older (B.11.00) box but will not run under ksh on
this new box (B.11.11).

error is: ksh: @: parameter not set

If anyone has any thoughts on this matter it would be much appreciated.

TIA

eddiec :-)

0
Reply chalk (34) 12/16/2005 1:39:33 AM

sounds like "set -u" is in effect (treat unset variables as error).
Echo $- in the script, if you see "u" in the list, then try the script
w/ "set +u" and see if that behaves as you want.

0
Reply scott 12/16/2005 3:06:58 PM


pc <chalk@netspace.net.au> wrote:
> hi all,
> 
> I am having a problem checking for the existence of an environment
> variable on an new HP-UX box (11.11) under ksh: This script for
> example:
> 
> if [ -n "$MYVAR" ] ; then
>         echo "MYVAR is set"
> else
>         echo "MYVAR is not set"
> fi
> 
> runs under ksh on an older (B.11.00) box but will not run under ksh on
> this new box (B.11.11).
> 
> error is: ksh: @: parameter not set
                 ^

  Why does it says "@"? I.e. what is "@" in your script or in some
input?

  BTW, the code snippet looks OK. It's essential to have quotes around
$MYVAR in the test ("[ ... ]") statement, in case MYVAR is not set, but,
if this is the actual code, you have that covered.

> If anyone has any thoughts on this matter it would be much appreciated.
> 
> TIA
> 
> eddiec :-)
0
Reply Frank 12/16/2005 8:02:43 PM

In article <43a31d62$0$287$dbd4b001@news.wanadoo.nl>, Frank Slootweg wrote:
> pc <chalk@netspace.net.au> wrote:
>> hi all,
>> 
>> I am having a problem checking for the existence of an environment
>> variable on an new HP-UX box (11.11) under ksh: This script for
>> example:
>> 
>> if [ -n "$MYVAR" ] ; then
>>         echo "MYVAR is set"
>> else
>>         echo "MYVAR is not set"
>> fi
>> 
>> runs under ksh on an older (B.11.00) box but will not run under ksh on
>> this new box (B.11.11).
>> 
>> error is: ksh: @: parameter not set
>                  ^
> 
>   Why does it says "@"? I.e. what is "@" in your script or in some
> input?
> 
>   BTW, the code snippet looks OK. It's essential to have quotes around
> $MYVAR in the test ("[ ... ]") statement, in case MYVAR is not set, but,
> if this is the actual code, you have that covered.

The "[ ... ]" is a Bourne-style test, not a Korn shell test ("[[ ... ]]").
There are differences in behavior, although that shouldn't matter in this case.
When coding for ksh, it is best to use "[[ ... ]]".


>> If anyone has any thoughts on this matter it would be much appreciated.
>> 
>> TIA
>> 
>> eddiec :-)

Kevin

-- 
Unix Guy Consulting, LLC
Unix and Linux Automation, Shell, Perl and CGI scripting
http://www.unix-guy.com
0
Reply Kevin 12/16/2005 10:52:45 PM

Kevin Collins <spamtotrash@toomuchfiction.com> wrote:
[deleted]

> The "[ ... ]" is a Bourne-style test, not a Korn shell test ("[[ ... ]]").

  Well, the Korn shell actually knows *both*. See the description of
'test' in the ksh(1) manual page:

ksh(1)> test [expr]   Evaluate conditional expression expr. See test(1)
ksh(1)>               for usage and description. 

and the test(1) manual page (of course) mentions:

test(1)> SYNOPSIS
test(1)> test expr 
test(1)> 
test(1)> [ expr ]
 
> There are differences in behavior, although that shouldn't matter in
> this case.

  True.

> When coding for ksh,

  One shouldn't "code for ksh"! :-)

> it is best to use "[[ ... ]]".

  Which is also in the (de jure) *standard* shell, the POSIX shell.
0
Reply Frank 12/17/2005 12:21:54 AM

"Frank Slootweg" <this@ddress.is.invalid> schrieb im Newsbeitrag 
news:43a31d62$0$287$dbd4b001@news.wanadoo.nl...
> pc <chalk@netspace.net.au> wrote:
>> hi all,
>>
>> I am having a problem checking for the existence of an environment
>> variable on an new HP-UX box (11.11) under ksh: This script for
>> example:
>>...
Hi !

There is something like :
${MyEnvVar?NowSet}

If MyEnvVar is set : nothing will be done ( but an error-message is sent to 
stderr )
If MyEnvVar ist not set : it will become "NowSet"

To prevent the error-message use :
Something=${MyEnvVar?NowSet}

My ksh-book is in the company, so I can not look for this topic.
I will look into the book on monday.

regards
  reinhard dot skarbal at aon dot at 


0
Reply nobody 12/17/2005 11:27:28 AM

hi everyone,

thank you very much for the feedback. Scott from Steelox was right, the
..profile file had set -u.

cheers,

eddiec :-)

0
Reply pc 12/18/2005 10:55:23 PM

"nobody" <nobody@nowhere.com> schrieb im Newsbeitrag 
news:43a3f636$0$11868$3b214f66@tunews.univie.ac.at...
>
> "Frank Slootweg" <this@ddress.is.invalid> schrieb im Newsbeitrag 
> news:43a31d62$0$287$dbd4b001@news.wanadoo.nl...
>> pc <chalk@netspace.net.au> wrote:
>>> hi all,
>>>
>>> I am having a problem checking for the existence of an environment
>>> variable on an new HP-UX box (11.11) under ksh: This script for
>>> example:
>>>...
> Hi !
>
> There is something like :
> ${MyEnvVar?NowSet}
>
> If MyEnvVar is set : nothing will be done ( but an error-message is sent 
> to stderr )
> If MyEnvVar ist not set : it will become "NowSet"
>
> To prevent the error-message use :
> Something=${MyEnvVar?NowSet}
>
> My ksh-book is in the company, so I can not look for this topic.
> I will look into the book on monday.
>
> regards
>  reinhard dot skarbal at aon dot at
>
To make my answer complete :
MyEnvVar=${MyEnvVar:=0}
If MyEnvVar ist not set : it will become 0
regards
   reinhard dot skarbal at aon dot at 


0
Reply nobody 12/20/2005 5:19:19 PM

7 Replies
165 Views

(page loaded in 1.21 seconds)

Similiar Articles:













7/28/2012 12:45:51 AM


Reply: