8

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

2
  • 2
    What you're asking is very unclear. It's also unclear why you are using grave accents in your script... remove them all and replace the last line with a simple "./odd222" (without the quotes) and I think things will work as you expect. Commented Sep 26, 2013 at 10:33
  • There are a few glitches in your script. First of all you should not have to make odd222 file executable. gcc should handle that by itself. Second don't echo your odd222's output. Try ./odd222 directly in your script. Commented Sep 26, 2013 at 10:37

4 Answers 4

13

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

Your last paragraph is wrong. The echo has nothing to do with the input the program receives.
see my revised answer.I didn't mean that
I'm afraid the sentences you added are even more confused. The first half of your answer is okay.
2

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:

Example Screenshot

2 Comments

Kotlin Compiler Script kotlinc pwd/"$1.kt" -include-runtime -d "$1.jar" java -jar "$1.jar"
why not cc instead? cc isn't portable, but it's an alias of either GCC or Clang.
1

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;
}

1 Comment

@JohnKugelman, thanks edited that, hope it sounds better and clearer now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.