4

I am writing a bash script to compile and run a C++ program. Here's my script

#!/bin/bash
PROG_NAME=$1
output=$(g++ $PROG_NAME)  #redirect the error to a variable
echo $output              #show the error on stdout
if [$output = ""] 
then
    ./a.out
fi

I don't want to run the a.out file if the program fails to compile. To do so, I have created a variable to store compile time error message. But this approach doesn't seem to work, since the output is not being redirected to the variable. Is there any other way to do it?

Edit

This is the script which worked for me

#!/bin/bash
PROG_NAME=$1
g++ -Werror $PROG_NAME
if [[ $? == 0 ]]; then
    ./a.out
fi
Community
  • 1
  • 1
Akash Karnatak
  • 678
  • 2
  • 7
  • 16
  • 1
    "Bash script to compile and run C++ program" BTW: That is what `make` is designed for. Compiling with handcrafted scripts is not a good idea at all. So you simply should start using the tools which are designed for the job you want to do! – Klaus Jan 16 '20 at 12:01

5 Answers5

3

If g++ fails it will return with a non-zero return-value which can be checked for with a Bash if command:

output=$(g++ $PROG_NAME 2>&1)
if [[ $? != 0 ]]; then
    # There was an error, display the error in $output
    echo -e "Error:\n$output"
else
    # Compilation successfull
    ./a.out
fi

A possibly better solution (IMO) is to learn how to use makefiles, as it will allow more complex projects as well.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

@AkashKarnatak : In your example, you don't redirect the error to a variable, you store the standard outpu of g++ into a variable.

Perhaps the simplest would be to let g++ indicate whether it was successful or not. This could be done by

g++ $PROG_NAME && ./a.out

This displays all errors and, since g++ sets a non-zero exit code on error, executes the resulting file only if there are no errors.

user1934428
  • 19,864
  • 7
  • 42
  • 87
1

Conventionally, programs write error output on STDERR.

The statement output=$(g++ $PROG_NAME) will fill the variable $output only with the STDOUT (which is empty in case of errors).

You have different options here.

Keeping your design, what you need to do it is to redirect the STDERR on STDOUT. Simply:

output=$(g++ $PROG_NAME 2>&1)
BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • 1
    You can redirect stdout to stderr and print `$output`. [The syntax is the opposite](https://stackoverflow.com/questions/2990414/echo-that-outputs-to-stderr) – BiagioF Jan 16 '20 at 12:06
0

You can check whether this executable file exist or not with:

if test -f "$FILE"; then
    echo "$FILE exist"
    ./$FILE
fi

Beginning of the script use this code for removing executable file from folder. Full content of run.sh:

#!/bin/bash
FILE=a.out
if test -f "$FILE"; then
    echo "$FILE removed!"
    rm $FILE
fi
PROG_NAME=$1
echo $PROG_NAME
output=$(g++ $PROG_NAME)  #redirect the error to a variable
echo $output              #show the error on stdout
if test -f "$FILE"; then
    echo "$FILE exist"
    ./$FILE
fi

Then run using:

bash run.sh main.cpp
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
hhuseyin
  • 21
  • 3
0

Since your goal is to compile a program then run it if the compilation succeeds, instead of writing a script it might be easier to use g++ program.cpp && ./a.out