Created
February 16, 2021 22:23
-
-
Save DSCF-1224/5f9c58c62e8db3d77dfb5c13c15f90b7 to your computer and use it in GitHub Desktop.
Fortran 2003の get_environment_variable の使用方法の確認
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! ================================================================================================================================== | |
! | |
! [reference] | |
! https://www.nag-j.co.jp/fortran/fortran2003/Fortran2003_8_14.html#AUTOTOC_8_14 | |
! https://www.nag-j.co.jp/fortran/tips/tips_GetEnvironmentVariable.html#_GetEnvironmentVariable | |
! | |
! ================================================================================================================================== | |
program main | |
! <module>s to import | |
use, intrinsic :: iso_fortran_env | |
! require all variables to be explicitly declared | |
implicit none | |
call show('FRED') | |
call show('USER') | |
call show('USERNAME') | |
! contained <subroutine>s and <function>s are below | |
contains | |
subroutine show (name) | |
! argument(s) for this <subroutine> | |
character(len=*) , intent(in) :: name | |
! variable(s) for this <subroutine> | |
character(len=:) , allocatable :: value_env_var | |
integer :: length_env_var | |
integer :: status_env_var | |
! STEP.01 | |
call get_environment_variable( name= name, status= status_env_var, length= length_env_var ) | |
! STEP.02 | |
select case (status_env_var) | |
case(0) | |
! when succeeded to get environment variable | |
allocate( character(len= length_env_var) :: value_env_var ) | |
call get_environment_variable( name= name, value= value_env_var ) | |
write(unit= OUTPUT_UNIT, fmt='("Environment variable `",A,"` has the value `",A,"`.")') name , value_env_var | |
case(1) | |
! when the target environment variable does not exist | |
write(unit= ERROR_UNIT, fmt='("Environment variable `",A,"` does not exist")') name | |
case default | |
! when the failed to get the target environment variable | |
write(unit= ERROR_UNIT, fmt='("Failed to get Environment variable `",A,"`.")') name | |
write(unit= ERROR_UNIT, fmt='("STATUS :",1X,I0)') status_env_var | |
end select | |
! STEP.END | |
return | |
end subroutine show | |
end program main | |
! ================================================================================================================================== | |
! EOF | |
! ================================================================================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment