Hello,

Sign up to join our community!

Welcome Back,

Please sign in to your account!

Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

BashCow Latest Questions

  • 20
  • 20
flangworth

How to write a bash script that compiles a program, sends output to a log, then checks if the compilation was successful?

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”.

Related Questions

1 Answer

  1. 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 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
    
    See less
Leave an answer

Leave an answer