C Library Functions  - get_env (3)

NAME

get_env(3f) - [M_io:QUERY] a function returning the value of an environment variable (LICENSE:PD)

CONTENTS

Syntax
Description
Options
Returns
Examples
See Also
Author
License

SYNTAX

function get_env(NAME,DEFAULT,IERR=IERR) result(VALUE)

    character(len=*),intent(in)            :: NAME

! ONE OF ... o character(len=*),intent(in),optional :: DEFAULT o real,intent(in),optional :: DEFAULT o integer,intent(in),optional :: DEFAULT o doubleprecision,intent(in),optional :: DEFAULT o logical,intent(in),optional :: DEFAULT

integer,intent(out),optional :: IERR

! ONE OF THE FOLLOWING, MATCHING TYPE OF DEFAULT o character(len=:),allocatable :: VALUE o integer :: VALUE o real :: VALUE o doubleprecision :: VALUE o logical :: VALUE

DESCRIPTION

Get the value of an environment variable or optionally return a default value when the environment variable is not set or is set to a blank string.

The type returned is the same as the type of the default value.

OPTIONS

NAME name of environment variable
DEFAULT
  value to return if environment variable is not set or set to an empty string. May be CHARACTER, REAL, INTEGER, LOGICAL or DOUBLEPRECISION. Defaults to a null CHARACTER value.

RETURNS

VALUE the value of the environment variable or the default. The type is the same as DEFAULT. If an error occurs and it is numeric, huge(0|0.0|0.0d0) is returned.

For a LOGICAL type, Any environment variable value starting with F,f,N or n is .FALSE. and any value starting with Y,y,T or t is true. A leading period (".") is ignored. Anything else returns .false. .

IERR return error code. Must be specified with a keyword. It is zero if no error occurred.

EXAMPLES

Sample program:

   program demo_get_env
   use M_io, only : get_env, getname
   character(len=*),parameter :: g=’(*(g0))’
   integer :: ierr
   character(len=:),allocatable :: HOME
     !Basics
      HOME=get_env(’HOME’,’UNKNOWN’)
      write(*,’(a)’)HOME
      write(*,’(a)’)Get_env(’PATH’)

!call this program setting STOP=RUN unless STOP=RUN !otherwise print various environment variable values !converted to various types if(get_env(’STOP’).eq.’RUN’)then write(*,g)repeat(’-’,80) write(*,g)get_env(’CHARACTER’,’string’) write(*,g)get_env(’INTEGER’,100) write(*,g)get_env(’REAL’,200.0) write(*,g)get_env(’DOUBLE’,300.0d0) write(*,g)get_env(’LOGICAL’,.true.)

write(*,g)repeat(’-’,80) write(*,g)get_env(’CHARACTER’,’string’,ierr=ierr) write(*,*)’ierr=’,ierr write(*,g)get_env(’INTEGER’,100,ierr=ierr) write(*,*)’ierr=’,ierr write(*,g)get_env(’REAL’,200.0,ierr=ierr) write(*,*)’ierr=’,ierr write(*,g)get_env(’DOUBLE’,300.0d0,ierr=ierr) write(*,*)’ierr=’,ierr write(*,g)get_env(’LOGICAL’,.true.) write(*,*)’ierr=’,ierr

write(*,g)repeat(’-’,80) write(*,g)get_env(’CHARACTER’) write(*,g)get_env(’HOME’) else write(*,g)repeat(’-’,80) call execute_command_line(’env STOP=RUN ’//getname()) call execute_command_line(’env STOP=RUN CHARACTER=aaaa & & INTEGER=1 REAL=2.3 DOUBLE=444444444444 ’//getname()) call execute_command_line(’env STOP=RUN CHARACTER=bbbb & & INTEGER=1 REAL=2.3 DOUBLE=44.555 ’//getname()) call execute_command_line(’env STOP=RUN CHARACTER=cccc & & INTEGER=asdf REAL=asdf DOUBLE=adsf ’//getname()) write(*,g)repeat(’-’,80) stop endif

end program demo_get_env

SEE ALSO

This duplicates system_getenv(3m_system) in most respects but avoids some interdependencies as M_system(3) currently requires a POSIX programming environment.

get_environment_variable(3fortran), system_getenv(3m_system), set_environment_variable(3m_system), system_putenv(3m_system), system_clearenv(3m_system), system_initenv(3m_system), system_getenv(3m_system), system_unsetenv(3m_system)

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 get_env (3) February 23, 2025
Generated by manServer 1.08 from dd398a2a-0035-4b32-9ebd-a640428d19cc using man macros.