Reputation: 95
I am using a subroutine call to GET_ENVIRONMENT_VARIABLE
to read the computer hostname in a Fortran program. I cannot read this variable, though it is okay if I read other variables, as $USER
. In my system (Debian Jessie, gfortran 4.9):
$ echo $HOSTNAME
deckard
$ echo $USER
curro
I have prepared this short program:
program hello
!
implicit none
integer :: ivar = 0, len, stat
character(LEN=256) :: host, user
call GET_ENVIRONMENT_VARIABLE('HOSTNAME', host, len, stat)
if (stat == 0) then
print*, "Hostname read: ", host
else
print*, "Hostname read failed: stat = ", stat
endif
call GET_ENVIRONMENT_VARIABLE('USER', user, len, stat)
if (stat == 0) print*, "Username read: ", user
print *, "This is user ", trim(user), " in node ", trim(host), "."
!
end program hello
If I run this simple program (compiled with or without the -std=f2003) the output is:
$ ./a.out
Hostname read failed: stat = 1
Username read: curro
This is user curro in node .
Therefore the error is stat = 1.
I know that gfortran has the intrinsic HOSTNM
but for compatibility with other compilers I would prefer GET_ENVIRONMENT_VARIABLE
. Any idea why is this happening?
Upvotes: 2
Views: 291
Reputation: 7293
I will suppose that your shell is bash but the end result should not change too much if the actual shell is another one.
In bash, you have access to environment variables and to regular bash variables. To make a variable accessible to child process (that is, programs that are launched by bash such as gfortran or your program 'a.out') it has to be part of the environment. By default, only variables from the posix set of environment variables are passed along (altough some may be omitted).
Examples of posix variables that are accessible both from the bash command line and from your fortran program (or any other program for that matter): HOME
, USER
, PWD
or PATH
.
You can verify that they are indeed accessible by checking what the env
program knows:
env | grep HOME
env | grep USER
while
env | grep HOSTNAME
returns nothing.
So you need to export the variable HOSTNAME to place it in the list of known environment variable. bash happens to know about hostname because it defines a set of variables for the convenience of the user (see https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-HOSTNAME ). Another example of convenience variables is BASH_VERSION
that is specific to bash itself.
export HOSTNAME
env | grep HOSTNAME
but the users of your program (or yourself but on a different computer) might not have access to it as an environment variable so this solution might not be of use to you. The "canonical" way of knowing the hostname without relying on the environment variable, on linux, is to execute the program "hostname".
Upvotes: 4
Reputation: 8140
Have you exported the HOSTNAME
variable?
Exporting means that it is available to the child processes as well. That might very well be the difference between the HOSTNAME
and USER
environment variables that you experience.
Upvotes: 1