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