Bash Fortran 代码的包装器

Bash wrapper for fortran code

我有一个 Fortran 代码(由其他人编写 - 无法更改...),它接受一个输入参数文件,执行它,然后有一个交互式提示。它是这样工作的:

[user@host] ./mycode

Welcome; what is the file name? _

一旦你给它参数文件并按下回车键,程序就会执行它并提示选项:

OPTIONS  a=add something
         u=undo
         o=overplot
         q=quit

然后您与代码交互,然后退出。我遇到的问题是,每次我退出程序并不得不重新开始时,我都必须不断地重新输入参数文件名(这对于长名称来说是一种痛苦)。我想写一个简单的 shell-script 来做:

./mycode_auto param_file

然后它会执行 param_file 并给出带有选项的提示。我的第一次幼稚尝试,我知道它遗漏了一些东西:

#!/bin/bash

./mycode << EOF



EOF

它打开 mycode,执行参数文件,但之后立即中断,我得到:

Fortran runtime error: End of file

我实际上可以理解它发生了什么,但不知道如何解决它。有什么想法吗?

谢谢!

如果你不能修改fortran程序,我相信你唯一的解决办法就是使用。看看下面的脚本:

#!/usr/bin/expect -f

#we store the content of our 1st argument
set file_path [lindex $argv 0]

#process we need to interract with
spawn ./mycode

#if we encounter this message ...
expect "Welcome; what is the file name?" {
    #... we send it our first argument
    send "$file_path\r"
}
#we resume normal interaction with our script
interact

简单地这样称呼它:script.expect "/path/to/file",假设 expect 脚本和 mycode 在同一个文件夹中。