GET_ENVIRONMENT_VARIABLE(3) - [SYSTEM:ENVIRONMENT] Get value of an environment variable
call get_environment_variable(name [,value] [,length] & & [,status] [,trim_name] [,errmsg] )
subroutine character(len=*) get_environment_variable( & & name, value, length, status, trim_name, errmsg )character(len=*),intent(in) :: name character(len=*),intent(out),optional :: value integer(kind=**),intent(out),optional :: length integer(kind=**),intent(out),optional :: status logical,intent(out),optional :: trim_name character(len=*),intent(inout),optional :: errmsg
o a kind designated as ** may be any supported kind for the type meeting the conditions described herein. o NAME, VALUE, and ERRMSG are a scalar character of default kind. o LENGTH and STATUS are integer scalars with a decimal exponent range of at least four. o TRIM_NAME is a scalar of type logical and of default kind.
GET_ENVIRONMENT_VARIABLE(3) gets the VALUE of the environment variable NAME.
Note that GET_ENVIRONMENT_VARIABLE(3) need not be thread-safe. It is the responsibility of the user to ensure that the environment is not being updated concurrently.
If running in parallel be aware It is processor dependent whether an environment variable that exists on an image also exists on another image, and if it does exist on both images whether the values are the same or different.
o NAME : The name of the environment variable to query. The interpretation of case is processor dependent.
o VALUE : The value of the environment variable being queried. If VALUE is not large enough to hold the data, it is truncated. If the variable NAME is not set or has no value, or the processor does not support environment variables VALUE will be filled with blanks. o LENGTH : Argument LENGTH contains the length needed for storing the environment variable NAME. It is zero if the environment variable is not set. o STATUS : STATUS is -1 if VALUE is present but too short for the environment variable; it is 1 if the environment variable does not exist and 2 if the processor does not support environment variables; in all other cases STATUS is zero. o TRIM_NAME : If TRIM_NAME is present with the value .false., the trailing blanks in NAME are significant; otherwise they are not part of the environment variable name.
Sample program:
program demo_getenv implicit none character(len=:),allocatable :: homedir character(len=:),allocatable :: varTypical Results:var=HOME homedir=get_env(var) write (*,(a,"=""",a,""""))var,homedir
contains
function get_env(name,default) result(value) ! a function that makes calling get_environment_variable(3) simple implicit none character(len=*),intent(in) :: name character(len=*),intent(in),optional :: default character(len=:),allocatable :: value integer :: howbig integer :: stat integer :: length length=0 value= if(name.ne.)then call get_environment_variable( name, & & length=howbig,status=stat,trim_name=.true.) select case (stat) case (1) print *, name, " is not defined in the environment. Strange..." value= case (2) print *, & "This processor does not support environment variables. Boooh!" value= case default ! make string of sufficient size to hold value if(allocated(value))deallocate(value) allocate(character(len=max(howbig,1)) :: value) ! get value call get_environment_variable( & & name,value,status=stat,trim_name=.true.) if(stat.ne.0)value= end select endif if(value.eq..and.present(default))value=default end function get_env
end program demo_getenv
HOME="/home/urbanjs"
Fortran 2003
GET_COMMAND_ARGUMENT(3), GET_COMMAND(3)
fortran-lang intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | get_environment_variable (3fortran) | April 28, 2024 |