GET_ENVIRONMENT_VARIABLE(3) - [SYSTEM:ENVIRONMENT] Retrieve the 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) retrieves 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.
When 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 : This argument contains the length needed to store the environment variable name. It is zero if the environment variable is not set. o STATUS : Returns
o -1 if value is present but too short to fit in the provided variable. o 1 if the environment variable does not exist o 2 if the processor does not support environment variables o and 0 in all other cases. o TRIM_NAME : If present and set to .false. the trailing blanks in name are significant; otherwise, they are not considered part of the environment variable name. o ERRMSG : is assigned a processor-dependent explanatory message if the optional argument STATUS is, or would be if present, assigned a positive value. Otherwise, it is unchanged.
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 use, intrinsic :: iso_fortran_env, only : stderr=>ERROR_UNIT 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) write(stderr,*) & & name, " is not defined in the environment. Strange..." value= case (2) write(stderr,*) & & "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 intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | get_environment_variable (3fortran) | November 02, 2024 |