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
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:
Also run your executable without echo
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.odd.c
For running a C language program using a gcc shell script, see the following:
It is also applicable to any language. Modify according to language and compiler.
Step 1:
Create any file having .sh extension (shell script)
For example: your_file_name.sh
Step 2:
Contents of file as follows:
Step 3:
Change permission for read, write, and execute file in terminal using the following command:
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).
Example Screenshot:
You need not do to
If you just write
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. This is because pressing the ENTER key is a signal to the system to start a new line, and thus echo repeats this signal.When you write
it does not recognize the command.Hence it waits there only.echo has nothing to do with our program.
Get rid of the backticks, the chmod, and the
echo
. All you need to do is rungcc
, then run your program.It'd also be good to only try to run the program if it compiles successfully. You can make it conditional by using
&&
.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:Or flush the output explicitly: