question on "getenv" function

From: Mary Haley (haley AT XXXXXX)
Date: Thu Mar 01 2001 - 14:02:50 MST


The question below was originally posted to ncarg-talk@ucar.edu. I
thought others might benefit from the answer, so I decided to post it
ncl-talk AT ucar.edu as well.

> According to
>
> http://ngwww.ucar.edu/ngdoc/ng/ref/ncl/functions/getenv.html,
>
> "The getenv function returns either an empty string when the shell
> environment variable does not exist, or the string value of the
> environment parameter requested."
>
> However, if I do a getenv on an undefined environment variable, I get
> a fatal error:
>
> fatal:getenv : (NDAYS) is not a defined environment parameter
>
> Something of a curioisity.
>
> Gary Strand

If "getenv" is passed an environment variable that doesn't exist, then
the value returned will be a missing value. The fatal error message
returned from "getenv" doesn't actually cause NCL to quit, which might
be a little misleading.

Gary later asked if there was a way to check the status of the return
value from the "getenv" function so that if it fails, he won't try to
access that environment variable.

To do this, you can check if it is missing with the "ismissing"
function as follows:

  if(.not.ismissing(getenv("FILENAME"))) then
    ...access the FILENAME environment variable...
  else
    ...do something else...
  end if
  
--Mary Haley
haley AT ucar.edu

P.S. For more information on ncl-talk or ncarg-talk, or to see some
archive messages, go to:

    http://ngwww.ucar.edu/ncl-talk/
    http://ngwww.ucar.edu/ncarg-talk/



This archive was generated by hypermail 2b29 : Tue Feb 19 2002 - 09:06:05 MST