运行C程序的Linux脚本 [英] linux script running c program
问题描述
我试图在linux中运行脚本来运行c程序 脚本如下
i tried to run a script in linux to run a c program the script was as follows
#!/bin/bash
`gcc odd.c -o odd222`
`chmod +x odd222`
echo `./odd222`
而奇数c是
main()
{
int i;
printf("enter the no.");
scanf("%d",&i);
printf("shiv");
}
但是问题是,当我运行此脚本时,所有scanf语句都将被执行,然后所有输出都会显示出来…….
but the problem is that when i run this script the all the scanf statement are executed then all the outputs are shown simentaniously....
如果我没有在./odd222
之前放置echo,则表示输入错误,找不到命令(输入" printf中的第一个元素.
if i do not put echo before ./odd222
then it says error enter command not found("enter" the first element in printf.
请帮助我
推荐答案
摆脱掉反引号,chmod和echo
.您所需要做的就是运行gcc
,然后运行您的程序.
Get rid of the backticks, the chmod, and the echo
. All you need to do is run gcc
, then run your program.
#!/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
修改C代码以确保立即打印输出也将是一件好事.输出通常是行缓冲的,这意味着只有在您写完最后一行换行符\n
的全行时才显示.您将要打印换行符:
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 \n
at the end. You'll want to either print a newline:
printf("enter the no.\n");
或显式刷新输出:
printf("enter the no.");
fflush(stdout);
这篇关于运行C程序的Linux脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!