0

I am trying to create this little program to help me, with only one command, to compile and run a C program within Ubuntu's terminal.

Trying to make it fancier, I added an argument to the bash file so I can use it for any C program I want. So this is how it is supposed to go:

  • Create a variable to store the name of the file
  • Use that variable to compile the program (to the same file name)
  • Use that same name to run the file.

Here is the code:

# usr/bin/bash
filename=$1
cc -o $filename "$filename.c"
./$filename.out

almost everything runs, the only problem I still have is in the last line:

./$filename.out

It doesn't seem to use the name of the variable inside the command which executes the final program.

I'm a noob at bash (let's say I haven't used it in months).

1
  • 4
    I guess the actual problem is that your cc command doesn't produce $filename.out but just $filename -- that being said, a shell script doesn't make a good build tool. You should read up on Makefiles instead. Commented Jul 26, 2017 at 11:14

3 Answers 3

6

cc -o foo will output foo not foo.out. You should also double-quote the variable expansions to prevent IFS-splitting and globbing:

filename=$1
cc -o "$filename" "$filename.c" &&
./"$filename"

Apart from that # /usr/bin/bash (unlike #!/usr/bin/bash) does nothing. It's a comment. The whole thing will be run the /bin/sh, not bash (but you don't need bash, anyway).

Sign up to request clarification or add additional context in comments.

5 Comments

I replaced # /usr/bin/bash by #!/usr/bin/bash and it didn't work as I expected. bash: ./runC_Script.sh: usr/bin/bash: bad interpreter: No such file or directory
@Chihab although it doesn't matter because you don't use any bash syntax -- you didn't put #!/usr/bin/bash but #!usr/bin/bash. You should stop being so sloppy.
I gotta look into this, seems important!
@Chihab You might not have bash in /usr/bin. It could be in /bin. I'd use /bin/sh (or /bin/sh -e to get aborts on failures by default) because it could be a much faster POSIX shell and a POSIX shell is all you need for this.
@Chihab as a general rule, prefer writing scripts with #!/bin/sh (they should therefore work with any sh-compatible shell and avoid bashisms). Only write #!/usr/bin/bash in scripts that explicitly need special bash syntax/features.
-1

You will need to use quotation marks:

./"$filename.out"

1 Comment

This, although a good thing to do, is not necessary in every case. It definitely isn't the source of OP's problem.
-2
#!/bin/sh
filename=$1
cc -o "${filename}.out" "${filename}.c"
"./${filename}.out"

4 Comments

I could have used eval as well, still I needed to add .out in the first argument of cc as in cc -o "$filename.out" "$filename.c" Thanks :D
You need to use ${var} syntax to concatinate string
eval is completely unnecessary here .. the best you could achieve is that it doesn't actively hurt.
@FelixPalmen Yes, for single argument not necessary

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.