C Shell:如何使用非命令行参数执行程序?

发布于 2024-07-06 22:10:07 字数 291 浏览 5 评论 0 原文

我的 $SHELL 是 tcsh。 我想运行一个 C shell 脚本,它将多次调用一个程序,每次都会更改一些参数。 我需要调用的程序是 Fortran 语言。 我不想编辑它。 该程序仅在执行时才接受参数,但在命令行上则不接受参数。 在脚本中调用程序后,程序将获得控制权(这是我目前陷入困境的地方,我永远无法退出,因为在程序进程停止之前脚本不会执行任何操作)。 此时,我需要向其传递一些变量,然后经过几次迭代后,我将需要 Ctrl+C 退出程序并继续执行脚本。

如何才能做到这一点?

My $SHELL is tcsh. I want to run a C shell script that will call a program many times with some arguments changed each time. The program I need to call is in Fortran. I do not want to edit it. The program only takes arguments once it is executed, but not on the command line. Upon calling the program in the script, the program takes control (this is where I am stuck currently, I can never get out because the script will not execute anything until after the program process stops). At this point I need to pass it some variables, then after several iterations I will need to Ctrl+C out of the program and continue with the script.

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜雨飘雪 2024-07-13 22:10:07

要添加@Toybuilder所说的内容,您可以使用“此处文档”。 也就是说,您的脚本可以将

./myfortranprogram << EOF
first line of input
second line of input
EOF

<”和“EOF”之间的所有内容提供给程序的标准输入(Fortran 是否仍然使用“read ( 5,*)" 从标准输入读取?)

因为我认为 @ephemient 的评论应该出现在答案中:

更多提示:<<“EOF”可防止
在此处文档主体中进行插值;
<<-EOF 删除所有前导选项卡(因此
您可以缩进此处文档以匹配
其周围环境),并且 EOF 可以是
替换为任何令牌。 空令牌
(<<"") 表示此处文档停止
在第一个空行。

我不确定这些扩展的可移植性如何,或者它们是否只是 tcsh 扩展 - 我自己只使用过 < 类型“此处文档”。

To add to what @Toybuilder said, you can use a "here document". I.e. your script could have

./myfortranprogram << EOF
first line of input
second line of input
EOF

Everything between the "<<EOF" and the "EOF" will be fed to the program's standard input (does Fortran still use "read (5,*)" to read from standard input?)

And because I think @ephemient's comment deserves to be in the answer:

Some more tips: <<'EOF' prevents
interpolation in the here-doc body;
<<-EOF removes all leading tabs (so
you can indent the here-doc to match
its surroundings), and EOF can be
replaced by any token. An empty token
(<<"") indicates a here-doc that stops
at the first empty line.

I'm not sure how portable those ones are, or if they're just tcsh extensions - I've only used the <<EOF type "here document" myself.

勿挽旧人 2024-07-13 22:10:07

您要使用的是 Expect

What you want to use is Expect.

清泪尽 2024-07-13 22:10:07

嗯,你能为你的 Fortran 代码提供重定向吗? 您可以使用输入创建一个临时文件,然后使用标准输入重定向 (<) 将其传入。

Uhm, can you feed your Fortran code with a redirection? You can create a temporary file with your inputs, and then pipe it in with the stdin redirect (<).

﹉夏雨初晴づ 2024-07-13 22:10:07

这是unix程序期望的工作,它可以很好地、轻松地交互式命令程序并响应它们的提示。

This is a job for the unix program expect, which can nicely and easily interactively command programs and respond to their prompts.

谁人与我共长歌 2024-07-13 22:10:07

在被告知我的问题几乎与这个问题重复后,我被送到这里。

FWIW,我在使用 csh C shell 脚本时遇到了类似的问题。

这段代码允许执行 custom_command 而无需获取任何输入参数:

foreach f ($forecastTimes)
    custom_command << EOF
        arg1=x$f;2
        arg2=ya
        arg3=z,z$f
        run
        exit
        EOF
end

我第一次尝试时它不起作用,但是在我退格了该代码部分中的所有空白之后,我删除了“<<” 和“EOF”。 我还将结束符“EOF”一直退到左边距。 之后它起作用了:

foreach f ($forecastTimes)
    custom_command <<EOF
        arg1=x$f;2
        arg2=ya
        arg3=z,z$f
        run
        exit
EOF
end

I was sent here after being told my question was close to being a duplicate of this one.

FWIW, I had a similar problem with a csh C shell script.

This bit of code was allowing the custom_command to execute without getting ANY input arguments:

foreach f ($forecastTimes)
    custom_command << EOF
        arg1=x$f;2
        arg2=ya
        arg3=z,z$f
        run
        exit
        EOF
end

It didn't work the first time I tried it, but after I backspaced out all of the white space in that section of the code I removed the space between the "<<" and the "EOF". I also backspaced the closing "EOF" all the way to the left margin. After that it worked:

foreach f ($forecastTimes)
    custom_command <<EOF
        arg1=x$f;2
        arg2=ya
        arg3=z,z$f
        run
        exit
EOF
end
我们的影子 2024-07-13 22:10:07

不是 tcsh 用户,但如果程序运行然后通过 stdin 读取命令,那么您可以使用 shell 重定向 < 为其提供所需的信息命令。 如果您使用 & 在后台运行它,则执行时不会阻塞。 然后你可以sleep一会儿,然后使用你拥有的任何工具(psgrepawk等) 来发现程序的 PID,然后使用 kill 向其发送 SIGTERM,这与执行 Ctrl-C

Not a tcsh user, but if the program runs then reads in commands via stdin then you can use shell redirection < to feed it the required commands. If you run it in the background with & you will not block when it is executed. Then you can sleep for a bit, then use whatever tools you have (ps, grep, awk, etc) to discover the program's PID, then use kill to send it SIGTERM which is the same as doing a Ctrl-C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文