简体   繁体   English

运行C程序的Linux脚本

[英]linux script running c program

i tried to run a script in linux to run ac program the script was as follows 我试图在linux中运行脚本以运行ac程序,脚本如下

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

and odd.c is 而奇数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 ./odd222 then it says error enter command not found("enter" the first element in printf. 如果我没有在./odd222之前./odd222那么它说错误,请输入命令not found(“输入” printf中的第一个元素)。

kindly help me 请帮助我

Get rid of the backticks, the chmod, and the echo . 摆脱反引号,chmod和echo All you need to do is run gcc , then run your program. 您所需要做的就是运行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. 修改您的C代码以确保立即打印输出也将是一件好事。 Output is usually line buffered, meaning it's only displayed once you write a full line with a newline \\n at the end. 输出通常行缓冲,这意味着一旦你写一个换行符全线它只是显示\\n结尾。 You'll want to either print a newline: 您将要打印换行符:

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

Or flush the output explicitly: 或显式刷新输出:

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

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. 外壳程序会根据确定文件需要执行的方式来尝试执行该程序。只需进行这些更改,您的代码就会起作用。

Putting echo returns a blank line on the display screen followed by the command prompt on the subsequent line. 放置echo将在显示屏上返回空白行,然后在下一行显示命令提示符。 This is because pressing the ENTER key is a signal to the system to start a new line, and thus echo repeats this signal. 这是因为按ENTER键是系统开始新行的信号,因此echo重复此信号。

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与我们的程序无关。

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 同时运行您的可执行文件而不回显

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

You can remove chmod line from your script as you have already changed the file to executable mode and there is no need of it everytime. 您可以从脚本中删除chmod行,因为您已经将文件更改为可执行模式,并且每次都不需要它。

odd.c 奇数

#include <stdio.h>

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

  return 0;
}

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: 第三步:

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: 屏幕截图示例:

屏幕截图示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM