【问题标题】:Bash wrapper for fortran codefortran 代码的 Bash 包装器
【发布时间】:2017-12-26 06:39:54
【问题描述】:

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

[user@host] ./mycode

Welcome; what is the file name? _

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

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

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

./mycode_auto param_file

然后它会执行 param_file 并给出选项提示。我的第一次天真尝试,我知道它缺少一些东西:

#!/bin/bash

./mycode << EOF

$1

EOF

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

Fortran runtime error: End of file

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

谢谢!

【问题讨论】:

    标签: bash shell heredoc


    【解决方案1】:

    如果你不能修改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",假设期望脚本和mycode在同一个文件夹中。

    【讨论】:

    • 我的想法完全一致。
    • 哇,我想它的效果再好不过了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2022-06-13
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多