Fortran 2003; Access to Command Arguments and Environment Variables

Fortran 2003 is a major extension to Fortran 90/95 including many useful features (link).

One significant feature, also available in the g95 compiler (www.g95.org) (pfs.gantep.edu.tr/linux/g95/), is access to command arguments; this allows a program to take data from the execution command line. Similarly access to environment variables allows a program to take data from the operating system environment variables.

Let's go directly to some basic examples.

A file name is taken from a command-line argument:

CHARACTER(LEN=20) :: FileName

! Take the first command argument as the filename
CALL GET_COMMAND_ARGUMENT(1,FileName)
OPEN(UNIT=20,FILE=TRIM(FileName),ACTION="WRITE")

! Write some output and close the file
WRITE(20,*) "some output ..."
CLOSE(20)

END

$ g95 myprog.f95 -o myprog
$ myprog list2.dat

the result is the creation of the file list2.dat

Command-line arguments are listed:

CHARACTER(LEN=200) :: Command
CHARACTER(LEN=10) :: Argument

! Output the complete command
CALL GET_COMMAND(Command)
PRINT *, "Command = ",TRIM(Command)

! Output each argument separately
DO I=1,COMMAND_ARGUMENT_COUNT()
  CALL GET_COMMAND_ARGUMENT(I,Argument)
  PRINT *, "Argument ", I, " = ",TRIM(Argument)
END DO

END

$ g95 myprog.f95 -o myprog
$ myprog cake kettle pan spoon

the output is:

Command = cake kettle pan spoon
Argument 1 = cake
Argument 2 = kettle
Argument 3 = pan
Argument 4 = spoon

A file name is taken from an environment variable.

CHARACTER(LEN=100) :: FileName
INTEGER :: Stat

! Get FileName from the environment variable $filename
! assuming that its value is not more than 100 characters.
CALL GET_ENVIRONMENT_VARIABLE(NAME="filename",VALUE=FileName,STATUS=Stat)

IF (Stat/=0) THEN
  PRINT *, "Environment variable 'filename' not set!"
  STOP
END IF

OPEN(UNIT=20,FILE=TRIM(FileName),ACTION="WRITE")

! Write some output and close the file
WRITE(20,*) "some output ..."
CLOSE(20)

END

$ g95 myprog.f95 -o myprog
$ export filename=/scratch/andrew/myprog.output
$ myprog

the result is the creation of the file /scratch/andrew/myprog.output

Function and Subroutines in Detail

COMMAND_ARGUMENT_COUNT ()
is an inquiry function that returns the number of command arguments as a default integer scalar.

CALL GET_COMMAND ([COMMAND,LENGTH,STATUS])
returns the entire command by which the program was invoked in the following INTENT(OUT) arguments:

COMMAND (optional) is a default character scalar that is assigned the entire command.

LENGTH (optional) is a default integer scalar that is assigned the significant length (number of characters) of the command.

STATUS (optional) is a default integer scalar that indicates success or failure.

CALL GET_COMMAND_ARGUMENT (NUMBER[,VALUE,LENGTH,STATUS])
returns a command argument.

NUMBER is a default integer INTENT(IN) scalar that identifies the required command argument.
Useful values are those between 0 and COMMAND_ARGUMENT_COUNT().

VALUE (optional) is a default character INTENT(OUT) scalar that is assigned the value of the command argument.

LENGTH (optional) is a default integer INTENT(OUT) scalar that is assigned the significant length (number of characters) of the command argument.

STATUS (optional) is a default integer INTENT(OUT) scalar that indicates success or failure.

CALL GET_ENVIRONMENT_VARIABLE (NAME[,VALUE,LENGTH,STATUS,TRIM_NAME])
obtains the value of an environment variable.
NAME is a default character INTENT(IN) scalar that identifies the required environment variable. The interpretation of case is processor dependent.

VALUE (optional) is a default character INTENT(OUT) scalar that is assigned the value of the environment variable.

LENGTH (optional) is a default integer INTENT(OUT) scalar. If the specified environment variable exists and has a value, LENGTH is set to the length (number of characters) of that value. Otherwise, LENGTH is set to 0.

STATUS (optional) is a default integer INTENT(OUT) scalar that indicates success or failure.

TRIM_NAME (optional) is a logical INTENT(IN) scalar that indicates whether trailing blanks in NAME are considered significant.


Extracts from here.
system@gantep.edu.tr
04/03/2005