Passing Variables from a Shell-Script to a Fortran 90 Program

大兔子大兔子 提交于 2021-02-17 02:56:11

问题


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:


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.




回答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:


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



来源:https://stackoverflow.com/questions/29591857/passing-variables-from-a-shell-script-to-a-fortran-90-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!