1

I'm stuck on this little problem. I was wondering if it is possible to pass a variable of a bash-shell script to a f90 code?

1
  • 2
    Wonder no more, it certainly is possible. Commented Apr 12, 2015 at 16:44

3 Answers 3

5

I am pretty sure it was discussed here before, but I cannot find an exact duplicate.

You can pass arguments directly as arguments to the program

  ./program arg1 arg2

you can retrieve the values in the program as character strings in Fortran 2003 using subroutines GET_COMMAND ARGUMENT and COMMAND_ARGUMENT_COUNT. Click on the links to get useful examples.

In older Fortran you have to use non-standard extensions, such as the subroutines GETARG and IARGC.

You can also read an environment variable which was set in the script

VAR1 = ...
VAR2 = ...
./program

using Fortran 2003 subroutine GET_ENVIRONMENT_VARIABLE. In older Fortran you have to use some non-standard extension, such as the subroutine GETENV.

You can also redirect a file to the standard input of the program and read the data using the READ statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this has been discussed many times here on SO. Even one of my own modest contributions (stackoverflow.com/questions/13843772/…) touches on how to read command line arguments with get_command_argument.
2

You can choose between the following possibilities:

  • In bash use export of the variable
    myvar="example"; export myvar
  • Add them as argument to the fortran call
    myFortran "${myvar}"
  • Write them to a file and read the file
    Worst solution, just to mention them all
  • Write it to stdin of fortran program
    echo "${myvar}" | myFortran

3 Comments

Maybe worth mentioning how to get that value in the Fortran program. Actually in strictly standard Fortran 90 the 1 and 2 are not possible, although there are on-standard extensions for that.
@Vladimir Thanks for adding thr Fortran details.
The export in combination with getenv worked for me, thanks a lot!
1

And you can use the following procedures to read an environment variable: get_environment_variable

https://gcc.gnu.org/onlinedocs/gfortran/GET_005fENVIRONMENT_005fVARIABLE.html

Or read the number and value of the command argument: command_argument_count,get_command_argument. See:

https://gcc.gnu.org/onlinedocs/gfortran/Intrinsic-Procedures.html#Intrinsic-Procedures

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.