Reputation: 791
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
Upvotes: 8
Views: 43327
Reputation: 367
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:
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:
Upvotes: 2
Reputation: 1420
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.
odd.c
#include <stdio.h>
int main()
{
int i;
printf("enter the no.");
scanf("%d",&i);
printf("shiv = %d", i);
return 0;
}
Upvotes: 0
Reputation: 371
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. 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
echo `./odd222`
it does not recognize the command.Hence it waits there only.echo has nothing to do with our program.
Upvotes: 1
Reputation: 361556
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);
Upvotes: 12