Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何执行bash命令";“出口”;在Fortran程序中?_Fortran_Intel Fortran - Fatal编程技术网

如何执行bash命令";“出口”;在Fortran程序中?

如何执行bash命令";“出口”;在Fortran程序中?,fortran,intel-fortran,Fortran,Intel Fortran,我想在Fortran程序中执行命令export$VAR=3。不幸的是,子例程system无法执行此操作。您能帮助我如何执行VAR的初始化吗?我使用的是iPort 11.1版。在英特尔Fortran中,您可以使用非标准扩展名SETNVQQ或调用相应的操作系统函数(Win API或POSIX) 没有Fortran标准方式 它只会影响当前进程和子进程 character(20) :: val call setenvqq("x=5") call get_environment_variab

我想在Fortran程序中执行命令
export$VAR=3
。不幸的是,子例程
system
无法执行此操作。您能帮助我如何执行
VAR
的初始化吗?我使用的是iPort 11.1版。

在英特尔Fortran中,您可以使用非标准扩展名
SETNVQQ
或调用相应的操作系统函数(Win API或POSIX)

没有Fortran标准方式

它只会影响当前进程和子进程

  character(20) :: val

  call setenvqq("x=5")
  call get_environment_variable(name="x",value=val)
  print *, val
  call execute_command_line("echo $x")
end


一种可能的解决方案是使用Fortran中的C例程和
iso_C_绑定
内部模块。通过C,您可以设置环境变量,由Fortran程序启动的任何程序都可以访问这些变量。这应该适用于大多数平台和编译器

C例行程序

#include <stdio.h>
#include <stdlib.h>     /* putenv, getenv */

void c_setgetenv ()
{
    char* p;
    putenv("x=100");
    p = getenv("x");
    if (p!=NULL)
        printf (" The variable is: %s\n",p);
}

在本例中,我没有从Fortran向C例程传递任何参数,但您可以根据需要自定义和处理这些参数。请注意,您需要编译C目标代码并将其链接到Fortran程序。

您使用的是哪个
Fortran
<代码>90或
95
或英特尔的?我正在使用fortran 95。你知道答案吗?我不是一个“fortran人”,所以很遗憾我帮不了你。我能做的最好的事情就是为你找到一些链接。这是一个可能对您有所帮助的类似示例。我认为您不能用Fortran定义环境变量。你可以得到它,但我不认为你可以设置它。我看到你以前没有接受任何答案。我建议你接受其他问题的一些答案。对于这个问题,等待更多可能的答案可能是明智的。那么,$VAR会一直保留到我的程序终止吗?不,不会。您必须从主程序启动其他程序。没有办法保持它的出口。
#include <stdio.h>
#include <stdlib.h>     /* putenv, getenv */

void c_setgetenv ()
{
    char* p;
    putenv("x=100");
    p = getenv("x");
    if (p!=NULL)
        printf (" The variable is: %s\n",p);
}
program main
    use, intrinsic :: iso_c_binding
    implicit none

    ! The interface for the C routine
    interface
        subroutine c_setgetenv () bind(c)
            import ! use declarations from host (implicit none, iso_c_binding)
        end subroutine c_setgetenv
    end interface

    character(len=20) :: val

    !! begin
    call c_setgetenv()
    call get_environment_variable(name="x",value=val)
    print *, "Fortran: ", val
end program main