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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With