Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux script running c program

Tags:

c

linux

bash

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`

and odd.c is

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....

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

kindly help me

like image 819
shiv garg Avatar asked Sep 26 '13 10:09

shiv garg


1 Answers

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

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");

Or flush the output explicitly:

printf("enter the no.");
fflush(stdout);
like image 183
John Kugelman Avatar answered Nov 15 '22 06:11

John Kugelman