I am working on a script with the following objective: upon receiving two inputs (1- log file name, 2- program name), the script should compile the program and log both the standard output and error messages to the specified log file.
If the compilation is successful, it should write “compile V” and return 0.
Otherwise, it should write “compile X” and return an error code.
Here is my attempt so far:
#!/bin/bash
gcc {$2}.c -Wall -g -o $2 > $1 2>&1
exit
I am unsure how to check if the compilation was successful and how to echo either “V” or “X”.
Here's how you can check the exit status of gcc: #!/bin/bash # Run the gcc command gcc "$2".c -Wall -g -o "$2" > "$1" 2>&1 # Get the exit status of gcc status=$? # Output the appropriate message based on the exit status if [ $status -eq 0 ]; then echo "compile V" else echo "compile X" fi # Return the exit status of gcc exit $status
Here’s how you can check the exit status of
See lessgcc
: