使用脚本运行程序,该程序在程序中运行多个命令(Using a script to run a program which run several commands within the program)

我有一个C程序,我编译成一个可执行文件。 我想创建一个启动C程序的脚本,运行脚本包含的几个命令并最终退出脚本。 C程序要求其输入(命令)作为用户指定的参数。

我尝试过这样的脚本:

#/bin/bash ./program command 1 command 2 .. quit # A quit command within the program

但程序似乎并不明白,在我开始执行后,以下命令应该是C程序的参数。

我试图检查我的程序的命令,但也许一个单独的C程序检查这将更好。 您如何建议进行调试?

I have a C program that I compile to make an executable file. I want to make a script that starts the C program, runs several commands that the script contains and eventually exits the script. The C program requires its inputs (commands) as user specified arguments.

I tried scripting something like this:

#/bin/bash ./program command 1 command 2 .. quit # A quit command within the program

But the program does not seem to understand that after I start the execution that the following commands should be arguments to the C program.

I tried to check the commands of my program, but maybe a separate C program that checks this would be better. How would you suggest that it be debugged?

最满意答案

您需要告诉shell脚本命令是否输入到程序中:

#!/bin/bash ./program << END command 1 command 2 .. quit END

<<运算符告诉shell,以下行被送到给定程序的stdin,直到找到一条只显示END 。

You need to tell the shell script that the commands are input to the program:

#!/bin/bash ./program << END command 1 command 2 .. quit END

The << operator tells the shell that the following lines are fed to stdin of the given program until it finds a line that says only END.

更多推荐