运行c程序的linux脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19025763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:55:56  来源:igfitidea点击:

linux script running c program

clinuxbash

提问by shiv garg

i tried to run a script in linux to run a c program the script was as follows

我试图在linux中运行一个脚本来运行ac程序,脚本如下

#!/bin/bash
`gcc odd.c -o odd222`
`chmod +x odd222`
echo `./odd222`

and odd.c is

和odd.c 是

main()
{
int i;
printf("enter the no.");
scanf("%d",&i);
printf("shiv");
}

but the problem is that when i run this script the all the scanf statement are executed then all the outputs are shown simentaniously....

但问题是,当我运行这个脚本时,所有的 scanf 语句都会被执行,然后所有的输出都会同时显示......

if i do not put echo before ./odd222then it says error enter command not found("enter" the first element in printf.

如果我没有在之前放回声,./odd222它会说错误输入命令未找到(“输入”printf中的第一个元素。

kindly help me

请帮助我

采纳答案by John Kugelman

Get rid of the backticks, the chmod, and the echo. All you need to do is run gcc, then run your program.

去掉反引号、chmod 和echo. 您需要做的就是运行gcc,然后运行您的程序。

#!/bin/bash
gcc odd.c -o odd222
./odd222

It'd also be good to only try to run the program if it compiles successfully. You can make it conditional by using &&.

如果程序编译成功,则仅尝试运行该程序也很好。您可以使用&&.

#!/bin/bash
gcc odd.c -o odd222 && ./odd222

It'd also be good to modify your C code to ensure the printouts are printed immediately. Output is usually line buffered, meaning it's only displayed once you write a full line with a newline \nat the end. You'll want to either print a newline:

修改您的 C 代码以确保立即打印输出也很好。输出通常是行缓冲的,这意味着它仅在您写完一行\n并在末尾处换行时才显示。您需要打印换行符:

printf("enter the no.\n");

Or flush the output explicitly:

或者显式刷新输出:

printf("enter the no.");
fflush(stdout);

回答by Arya

You need not do to

你不需要做

echo `./odd222`

If you just write

如果你只是写

./odd222

The shell tries to execute the program according to how it determines the file needs to be executed.Just make these changes,your code will work.

shell 会根据它如何确定需要执行的文件来尝试执行程序。只需进行这些更改,您的代码就会工作。

Putting echoreturns a blank line on the display screen followed by the command prompt on the subsequent line. This is because pressing the ENTER key is a signal to the system to start a new line, and thus echo repeats this signal.

Puttingecho在显示屏幕上返回一个空行,随后是下一行的命令提示符。这是因为按下 ENTER 键是系统开始新行的信号,因此回声重复该信号。

When you write

当你写

echo `./odd222`

it does not recognize the command.Hence it waits there only.echo has nothing to do with our program.

它不能识别命令。因此它只在那里等待。echo 与我们的程序无关。

回答by user1502952

Here are few improvements:

这里有一些改进:

Remove inverted quotes in your script, No need of them.

删除脚本中的倒引号,不需要它们。

These are used when you want to store the return value of command in a variable.

当您想将命令的返回值存储在变量中时使用这些。

Example:

例子:

var=`gcc odd.c -o odd222`
echo $var # this prints the gcc command output

Also run your executable without echo

还可以在没有 echo 的情况下运行您的可执行文件

gcc odd.c -o odd222
chmod +x odd222
./odd222

You can remove chmodline from your script as you have already changed the file to executable mode and there is no need of it everytime.

您可以chmod从脚本中删除行,因为您已经将文件更改为可执行模式,并且每次都不需要它。

odd.c

奇数.c

#include <stdio.h>

int main()
{
  int i;
  printf("enter the no.");
  scanf("%d",&i);
  printf("shiv = %d", i);

  return 0;
}

回答by AKASH GUDADHE

For running a C language program using a gcc shell script, see the following:

要使用 gcc shell 脚本运行 C 语言程序,请参阅以下内容:

It is also applicable to any language. Modify according to language and compiler.

它也适用于任何语言。根据语言和编译器进行修改。

Step 1:

第1步:

Create any file having .sh extension (shell script)

创建任何具有 .sh 扩展名的文件(shell 脚本)

For example: your_file_name.sh

例如:your_file_name.sh

Step 2:

第2步:

Contents of file as follows:

文件内容如下:

gcc `pwd`/"$filename.c"

./"a.out"

Step 3:

第 3 步:

Change permission for read, write, and execute file in terminal using the following command:

使用以下命令更改终端中读取、写入和执行文件的权限:

sudo chmod 777 filename.c

Step 4:

第四步:

Execute file on terminal.

在终端上执行文件。

You must run the program from the directory where your source file is present because I have used the present working directory (if you want to select any spec).

您必须从源文件所在的目录运行程序,因为我使用了当前的工作目录(如果您想选择任何规范)。

./your_file_name.sh     filename.c

Example Screenshot:

示例截图:

Example Screenshot

示例截图