Reputation: 2842
I would like to use the value of the $HOSTNAME environment variable in my Fortran code.
My attempt to do so is:
CHARACTER(LEN=100) :: hostname
INTEGER :: status_value = 0
CALL GET_ENVIRONMENT_VARIABLE("hostname",hostname, STATUS=status_value)
IF (status_value == 2) THEN
WRITE(nout,*) 'WARNING: Processor does not support environment variables - hostname is unknown.'
hostname = 'Unknown'
ELSE IF (status_value == -1) THEN
WRITE(nout,*) 'WARNING: Hostname is too long for character variable - hostname is truncated.'
ELSE IF (status_value == 1) THEN
WRITE(nout,*) 'WARNING: $HOSTNAME environment variable does not exist - hostname is unknown.'
hostname = 'Unknown'
But my result is that I always get the result that $HOSTNAME does not exist.
My interpretation of the status value is based on this: http://gcc.gnu.org/onlinedocs/gfortran/GET_005fENVIRONMENT_005fVARIABLE.html
Interestingly, the example at the above page uses the 'HOME' environment variable. This environment variable works for me.
But I see no reason why $HOSTNAME should not:
MacBook-Pro:1N45 emiller$ echo $HOSTNAME # My shell prompt
MacBook-Pro.local
What is going on? What environment does my Fortran program see at runtime?
For what it's worth, I am using iFort 12.
Upvotes: 3
Views: 1613
Reputation: 50937
This actually works fine, but on most platforms, these environment variable names are case sensitive. I get your results on my linux box with gfortran or ifort, but if I change your "hostname" string to "HOSTNAME" I get the expected results.
Upvotes: 9