Kornshell: Testing if environment variable exists?

Kornshell: Testing if environment variable exists?

Post by lynt » Sat, 22 Feb 2003 04:54:37



Greetings:

    Under tcsh I can do:

   if (! $?FOO ) then
      setenv FOO bar
   endif

   How do I do this with the Kornshell? I've tried:

   ${FOO-bar}
   ${FOO:-bar}
   etc....

   I just keep getting an error about trying to excute the
   default string.

    Thanks
      Tom

 
 
 

Kornshell: Testing if environment variable exists?

Post by Ben » Sat, 22 Feb 2003 05:16:41



> Greetings:

>     Under tcsh I can do:

>    if (! $?FOO ) then
>       setenv FOO bar
>    endif

>    How do I do this with the Kornshell? I've tried:

if [[ -z $foo ]]; then
     export foo=bar
fi

or

[[ -z $foo ]] && export foo=bar

regards,
Ben

 
 
 

Kornshell: Testing if environment variable exists?

Post by Barry Margoli » Sat, 22 Feb 2003 05:01:26




>Greetings:

>    Under tcsh I can do:

>   if (! $?FOO ) then
>      setenv FOO bar
>   endif

>   How do I do this with the Kornshell? I've tried:

>   ${FOO-bar}
>   ${FOO:-bar}
>   etc....

>   I just keep getting an error about trying to excute the
>   default string.

That's because ${FOO-bar} doesn't just set the variable, it also *expands*
to the value of the variable.  Then the command line is executed after
variable substitution has been performed, so you have a command line that
begins with the value of the variable.  If you don't want this, you need to
provide a command to execute.  The ":" command, which does nothing, is
useful for this:

: ${FOO-bar}

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Kornshell: Testing if environment variable exists?

Post by Ben » Sat, 22 Feb 2003 05:30:56





>>Greetings:

>>   Under tcsh I can do:

>>  if (! $?FOO ) then
>>     setenv FOO bar
>>  endif

>>  How do I do this with the Kornshell? I've tried:

>>  ${FOO-bar}
>>  ${FOO:-bar}
>>  etc....

>>  I just keep getting an error about trying to excute the
>>  default string.

> That's because ${FOO-bar} doesn't just set the variable, it also *expands*
> to the value of the variable.  Then the command line is executed after
> variable substitution has been performed, so you have a command line that
> begins with the value of the variable.  If you don't want this, you need to
> provide a command to execute.  The ":" command, which does nothing, is
> useful for this:

> : ${FOO-bar}

Probably mean:
        : ${FOO=bar}

regards,
Ben

 
 
 

Kornshell: Testing if environment variable exists?

Post by Barry Margoli » Sat, 22 Feb 2003 05:24:40





>> Greetings:

>>     Under tcsh I can do:

>>    if (! $?FOO ) then
>>       setenv FOO bar
>>    endif

>>    How do I do this with the Kornshell? I've tried:

>if [[ -z $foo ]]; then
>     export foo=bar
>fi

That makes no distinction between a nonexistent variable and a variable
whose value is the null string.

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Kornshell: Testing if environment variable exists?

Post by Stephane CHAZELA » Sat, 22 Feb 2003 08:15:05



Quote:>     Under tcsh I can do:

>    if (! $?FOO ) then
>       setenv FOO bar
>    endif

[...]

[ "y${FOO+es}" = yes ] || FOO=bar export FOO

Or simply:

: ${FOO=bar}
export FOO

But don't do:

[ "n${FOO-o}" = no ] && FOO=bar

FOO could be set but already equal to "o", you don't have the
same problem with y${FOO+es}.

Note that just checks wether a variable is set, not wether it is
in the environment or not.

--
Stphane

 
 
 

Kornshell: Testing if environment variable exists?

Post by Ben » Sat, 22 Feb 2003 09:22:20





>>if [[ -z $foo ]]; then
>>    export foo=bar
>>fi

> That makes no distinction between a nonexistent variable and a variable
> whose value is the null string.

Well...:
    (${foo?}) 2>/dev/null
    if [[ $? -ne 0 ]]; then
       export foo=bar
    fi

regards,
Ben

 
 
 

Kornshell: Testing if environment variable exists?

Post by Ben » Sat, 22 Feb 2003 09:26:15






>>> if [[ -z $foo ]]; then
>>>    export foo=bar
>>> fi

>> That makes no distinction between a nonexistent variable and a variable
>> whose value is the null string.

> Well...:
>    (${foo?}) 2>/dev/null
>    if [[ $? -ne 0 ]]; then
>       export foo=bar
>    fi

Or...
    if [[ -z $((set;env) | grep "^foo=") ]]; then
       export foo=bar
    fi

Or...
    [[ -z $((set;env) | grep "^foo=") ]] && export foo=bar

regards,
Ben

 
 
 

Kornshell: Testing if environment variable exists?

Post by Tim Cargi » Sat, 22 Feb 2003 10:35:57



> Greetings:

[ ... gleefully deleted t/csh ... ]

Quote:

>    How do I do this with the Kornshell? I've tried:

[ ... deleted guess-code ... ]

Well ... Todo, we're not in t/csh anymore ...

Quote:

>    I just keep getting an error about trying to excute the
>    default string.

There are a couple of ways to test if is null and then
set it to a value if it is, but I always have to do a
'man test' and a 'man ksh' because I forget.  Sometimes
I go here:  http://incres.anu.edu.au/manuals/korn.html
when I forget.

Quote:

>     Thanks
>       Tom

HTH

Tim PITA

 
 
 

Kornshell: Testing if environment variable exists?

Post by Stephane CHAZELA » Sat, 22 Feb 2003 17:53:57


[...]
Quote:>>    (${foo?}) 2>/dev/null

[...]

That will fail if foo is set but doesn't resolv to a command
that exits with a 0 exit status.

Quote:>     if [[ -z $((set;env) | grep "^foo=") ]]; then
>        export foo=bar
>     fi

That's not reliable, variable values can have newline characters
in them. env variable names also.

A='
foo='

env 'A
foo=' ...

And you should put a space between '((' or some shells may
bark against that incorrect $((...)) syntax.

To test wether a shell variable is set:

[[ -n ${foo+yes} ]]
[ "y${foo+es}" = yes ] # in a Bourne shell
{ : ${foo?}; } 2> /dev/null
( set -u; : $foo ) 2> /dev/null

Note that with zsh and GNU grep, when you do:
set | grep whatever, you get: "Binary file (standard input)
matches", as IFS (by default) (at least) contains '\0'.

--
Stphane

 
 
 

1. Testing if an Environment variable exists in the csh on solaris 2.5

Hi,

I have a problem in my .cshrc on a solaris 2.5 machine. In the .cshrc i want
to check if i'm on the console of the machine using X11, or if I'm on an X11
terminal.
I tried to check if the environment variable REMOTEHOST was set. This ENV
variable is set if I make a connection via xrsh. If I start a normal xterm
session on the console (in an X11 session) the environment variable
REMOTEHOST is not set.
If I reference this REMOTEHOST variable and it is not 'setenv' I get the error:

REMOTEHOST: Undefined variable.
I tried to do this test via the $status after this echo command.
if ( $status) then
setenv DISPLAY 'unix:0.0'
else
setenv DISPLAY $REMOTEHOST':0.0'
endif

As soon as the shell sees the REMOTEHOST, and it is not there, the run stops,
and all the rest of the commands in .cshrc is not executed.

Any help is appreciated.

--

M.vr.Gr.
Tommy,

 ...................              Tom Adriaansen : Postmaster/System Manager
 .            _+_  .  
 .           (***) .   Address    University Computer Centre (KUN/UCI),
 ............/===\..              Geert Groote Plein Zuid 41,
 . KKKKKUUUUUNNNNN .              6525 GA Nijmegen,
 . KKK  UUUUU NNNN .              The Netherlands.
 . K    UUUUU  NNN .   Phone      +31-(24)-3617958 (bgg 3617957/3617949)
 . KKK  UUUUU   NN .   FAX        +31-(24)-3617979

 ...................

2. fonts problem

3. IFS environment variable

4. Disksuite problems

5. How to test if files exist in a path variable?

6. PC-LPM-16 A/D converter (anyone have a driver?)

7. Testing whether a variable exists or not.

8. Color Depth and Resolution in XFree86

9. Testing Whether an environment variable contains integer data in BASH

10. test test test test test test test

11. Why does the cgi environment not inherit environment variables?

12. how to export shell environment variables to the primary environment?

13. c shell script; Howto use environement variables to build environment variables and take their value