[comp.unix.questions] Environment variables

mike@turing.UNM.EDU.unm.edu (Michael I. Bushnell) (08/21/87)

How can I determine if an environment variable is set using csh?
(I.e., in a script)  

The manual doesn't seem to say.

					Michael I. Bushnell
					a/k/a Bach II
					mike@turing.UNM.EDU
---
I just had a NOSE JOB!!
				-- Zippy the Pinhead

roy@phri.UUCP (Roy Smith) (08/25/87)

In <626@unmvax.unm.edu> mike@turing.UNM.EDU.UUCP (Michael I. Bushnell) writes:
> How can I determine if an environment variable is set using csh?
> (I.e., in a script)  

	After a bit of experimentation, I came up with the following

----------------
#!/bin/csh

if (`printenv FOO`X == X) then
	echo FOO is not set
else
	echo FOO is set
endif

if (`printenv TERM`X == X) then
	echo TERM is not set
else
	echo TERM is set
endif
----------------

which gives me:

FOO is not set
TERM is set

	This works, but is a bit grotty.  I suspect there has to be a
prettier way to do it, but I don't see any.
-- 
Roy Smith, {allegra,cmcl2,philabs}!phri!roy
System Administrator, Public Health Research Institute
455 First Avenue, New York, NY 10016

tim@brspyr1.BRS.Com (Tim Northrup) (08/26/87)

in article <626@unmvax.unm.edu>, Michael I. Bushnell says:
+ 
| How can I determine if an environment variable is set using csh?
| (I.e., in a script)  
| 					Michael I. Bushnell
| 					a/k/a Bach II
+ 					mike@turing.UNM.EDU

	if ( $?variable ) then
		VARIABLE IS SET TO SOMETHING
	else
		IT AIN'T
	endif
-- 
Tim "The Enchanter" Northrup
========================================++=====================================
" ... for death awaits you all 		||	tim@brspyr1.BRS.Com
	with big, sharp, pointy teeth!"	||	uunet!steinmetz!brspyr1!tim

dupuy@amsterdam.columbia.edu (Alexander Dupuy) (08/26/87)

In article <2876@phri.UUCP> roy@phri.UUCP (Roy Smith) writes:
>In <626@unmvax.unm.edu> mike@turing.UNM.EDU.UUCP (Michael I. Bushnell) writes:
>> How can I determine if an environment variable is set using csh?
>> (I.e., in a script)  
>
>	After a bit of experimentation, I came up with the following
>
[c-shell script using `printenv` deleted]


maybe this is a stupid answer, but have you tried $?VARIABLE ?

% echo $?TERM
1
% echo $?FOO
0

I know I have a ( hacked) version of the 4.3 csh, but it does work for me.

@alex
---
arpanet: dupuy@columbia.edu
uucp:	...!seismo!columbia!dupuy

rsalz@bbn.com (Richard Salz) (08/26/87)

The problem is in distinguishing between "set var value" and
"setenv var value" -- in most cases, the C shell hides any difference
between them -- $var will get the value of the "var" variable, be it
a shell variable or an environment variable.

With that caveat in mind:
	% if ( $?TERM ) echo TERM is set
	TERM is set
	% if ( $?FOO ) echo FOO is set
	% set FOO=yow
	% if ( $?FOO ) echo FOO is now set
	FOO is now set
	% if ( $?BAR ) echo BAR is set
	% setenv BAR value
	% if ( $?BAR ) echo BAR is now set
	BAR is now set


Roy's idea of using printenv is probably better, but his method will probably
cause quoting problems if you try to see if TERMCAP is set after using
tset(1); this seems to do it:
	% if ( `printenv TERMCAP | wc -l` == 1 ) echo TERMCAP is set
	TERMCAP is set
	% if ( `printenv CAP | wc -l` == 1 ) echo CAP is set
	%

Hope this helps,
	/r$
-- 
For comp.sources.unix stuff, mail to sources@uunet.uu.net.

mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (08/26/87)

->>> How can I determine if an environment variable is set using csh?
...
-> maybe this is a stupid answer, but have you tried $?VARIABLE ?
-> 
-> % echo $?TERM
-> 1
-> % echo $?FOO
-> 0

This isn't quite right if you are unfortunate enough to have done:

	% set TERM = whatever	# a csh var., not an env. var.

Mike Khaw
-- 
internet:  mkhaw@teknowledge-vaxc.arpa
usenet:	   {hplabs|sun|ucbvax|decwrl|sri-unix}!mkhaw%teknowledge-vaxc.arpa
USnail:	   Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

mlandau@bbn.com (Matt Landau) (08/26/87)

In <626@unmvax.unm.edu> mike@turing.UNM.EDU.UUCP (Michael I. Bushnell) writes:
> How can I determine if an environment variable is set using csh?

Well, under 4BSD and Sun csh, this works:

	if ($?VAR) then
		echo "VAR is set"
	else
		echo "VAR is not set"
	endif

I would assume it works the same way in SysV, but I don't have one handy
to check.
-- 
 Matt Landau			A rock feels no pain...
 mlandau@bbn.com			...and an island never cries

dan@rose3.Rosemount.COM (Dan Messinger) (08/26/87)

In article <626@unmvax.unm.edu> mike@turing.UNM.EDU.UUCP (Michael I. Bushnell) writes:
>
>How can I determine if an environment variable is set using csh?
>(I.e., in a script)  

The following (as described in my manual) works fine.  However, it does
not differentiate between environment variables and shell variables.

#
if ( $?xxx ) then
	echo its set
else
	echo not set
endif

Dan Messinger
dan@rose3.rosemount.com

mikep@ism780c.UUCP (Michael A. Petonic) (08/27/87)

In article <2876@phri.UUCP> roy@phri.UUCP (Roy Smith) writes:
>In <626@unmvax.unm.edu> mike@turing.UNM.EDU.UUCP (Michael I. Bushnell) writes:
>> How can I determine if an environment variable is set using csh?
>> (I.e., in a script)  
>
>	After a bit of experimentation, I came up with the following
>
 [...]

A better was is to use printenv and the "status" variable, like this:
	printenv FOO > /dev/null
	if ( $status == 1 ) then
		variable is not set
	else
		variable is set
	fi

or something like that.  Look at the man page near the bottom for
the return values of printenv.

MikeP

guy%gorodish@Sun.COM (Guy Harris) (08/27/87)

> I would assume it works the same way in SysV, but I don't have one handy
> to check.

It definitely doesn't work the same way in vanilla SysV, since it doesn't have
"csh".  If a System V system offers the 4BSD "csh", it should work the same
way, although there is no absolute guarantee of this; somebody may have changed
or broken it.  (The same is true of 4BSD-derived systems.  BTW, before anybody
pipes up with this, Sun did *not* change the C shell in recent releases not to
change the "term" shell variable if the "TERM" environment variable is changed,
as has been claimed elsewhere.  That behavior is exhibited by straight 4.3BSD,
and I believe by straight 4.2BSD as well.)
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

henry@garp.mit.edu (Henry Mensch) (08/27/87)

roy@phri.UUCP (Roy Smith) wrote: 
->In <626@unmvax.unm.edu> mike@turing.UNM.EDU.UUCP (Michael I. Bushnell) writes:
->> How can I determine if an environment variable is set using csh?
->> (I.e., in a script)  
->
->	After a bit of experimentation, I came up with the following
->
-> . . .  (shell script removed here)
->
->	This works, but is a bit grotty.  I suspect there has to be a
->prettier way to do it, but I don't see any.

Yeah; try this:

61 garp /users/henry --> printenv PRINTER ; set in my .{login,cshrc}
ln03-bldge40-2
62 garp /users/henry --> printenv DEADLY ; some random name
63 garp /users/henry --> echo ${?PRINTER} ${?DEADLY}
1 0
64 garp /users/henry --> 

${?variablename} returns 1 if set and 0 if not set.  Oh yeah; this was
described in the manual page for csh that I have.

# Henry Mensch / <henry@garp.mit.edu> / E40-379 MIT, Cambridge, MA
#      {ames,cca,rochester,harvard,mit-eddie}!garp!henry

mouse@mcgill-vision.UUCP (09/06/87)

In article <159@papaya.bbn.com>, rsalz@bbn.com (Richard Salz) writes:
> The problem is in distinguishing between "set var value" and
> "setenv var value" [...].

> Roy's idea of using printenv is probably better, but his method will
> probably cause quoting problems if you try to see if TERMCAP is set
> after using tset(1); this seems to do it:

> 	% if ( `printenv TERMCAP | wc -l` == 1 ) echo TERMCAP is set
> 	TERMCAP is set
> 	% if ( `printenv CAP | wc -l` == 1 ) echo CAP is set
> 	%

Or even just

if { printenv TERMCAP >& /dev/null } then
....
endif

WARNING if you try this:  The following does NOT work correctly.

if { printenv TERMCAP >& /dev/null } echo TERMCAP is set

This will print nothing regardless of whether TERMCAP is set or not,
becuase, believe it or not, the echo command gets its output redirected
by the redirection in the {}!  (Verified by changing /dev/null to
/tmp/foo - system is mtXinu 4.3+NFS, though the bug is present in a
local derivative of the 4.2 csh as well, and therefore is presumably
present in the 4.2 csh.)  The version using then and endif does not
suffer from the same problem.

					der Mouse

				(mouse@mcgill-vision.uucp)