我的 $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?
发布评论
评论(6)
要添加@Toybuilder所说的内容,您可以使用“此处文档”。 也就是说,您的脚本可以将
“
<”和“
EOF
”之间的所有内容提供给程序的标准输入(Fortran 是否仍然使用“read ( 5,*)" 从标准输入读取?)因为我认为 @ephemient 的评论应该出现在答案中:
我不确定这些扩展的可移植性如何,或者它们是否只是 tcsh 扩展 - 我自己只使用过
< 类型“此处文档”。
To add to what @Toybuilder said, you can use a "here document". I.e. your script could have
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:
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.您要使用的是 Expect。
What you want to use is Expect.
嗯,你能为你的 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 (<).
这是unix程序期望的工作,它可以很好地、轻松地交互式命令程序并响应它们的提示。
This is a job for the unix program expect, which can nicely and easily interactively command programs and respond to their prompts.
在被告知我的问题几乎与这个问题重复后,我被送到这里。
FWIW,我在使用 csh C shell 脚本时遇到了类似的问题。
这段代码允许执行 custom_command 而无需获取任何输入参数:
我第一次尝试时它不起作用,但是在我退格了该代码部分中的所有空白之后,我删除了“<<” 和“EOF”。 我还将结束符“EOF”一直退到左边距。 之后它起作用了:
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:
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:
不是
tcsh
用户,但如果程序运行然后通过stdin
读取命令,那么您可以使用 shell 重定向<
为其提供所需的信息命令。 如果您使用&
在后台运行它,则执行时不会阻塞。 然后你可以sleep
一会儿,然后使用你拥有的任何工具(ps
、grep
、awk
等) 来发现程序的 PID,然后使用kill
向其发送SIGTERM
,这与执行 Ctrl-C。Not a
tcsh
user, but if the program runs then reads in commands viastdin
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 cansleep
for a bit, then use whatever tools you have (ps
,grep
,awk
, etc) to discover the program's PID, then usekill
to send itSIGTERM
which is the same as doing a Ctrl-C.