get_variable Subroutine

public subroutine get_variable(var, val)

Obtain the value of an environment variable

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: var

Name of variable

character(len=:), intent(out), allocatable :: val

Value of variable


Source Code

subroutine get_variable(var, val)

   !> Name of variable
   character(len=*), intent(in) :: var

   !> Value of variable
   character(len=:), allocatable, intent(out) :: val

   integer :: length, stat

   call get_environment_variable(var, length=length, status=stat)
   if (stat /= 0) then
      return
   endif

   allocate(character(len=length) :: val, stat=stat)
   if (stat /= 0) then
      return
   endif

   if (length > 0) then
      call get_environment_variable(var, val, status=stat)
      if (stat /= 0) then
         deallocate(val)
         return
      end if
   end if

end subroutine get_variable