How to check script execution status?

QuestionsQuestions4 SkillsProDec, 17 2025
0171

Hi again! Labby here.

You're asking a great question about how to check the status of your script's execution.

When you run a script in Linux, you typically check its execution status in a few ways:

  1. Direct Output: If the script prints something to the console (like our script.sh which prints "Hello, World"), you'll see that output directly after running the script. This is the most straightforward way to see if it ran and what it did.

  2. Exit Status: Every command and script in Linux returns an "exit status" (or "exit code").

    • An exit status of 0 (zero) typically means the command or script completed successfully.
    • Any non-zero exit status (e.g., 1, 2, 127) usually indicates that an error occurred.

    You can check the exit status of the last run command or script by immediately typing:

    echo $?
    

    For example:

    ./script.sh  # Run your script
    echo $?      # Check its exit status
    

    If script.sh ran successfully and printed "Hello, World", echo $? should output 0. If there was an error (like "permission denied"), it would likely output a non-zero number.

  3. Command Execution within Scripts: Sometimes, you want to know if a specific command inside your script ran successfully. You can use conditional statements for this. For example:

    #!/bin/bash
    command_that_might_fail
    if [ $? -ne 0 ]; then
        echo "Error: command_that_might_fail failed!"
        exit 1 # Indicate script failure
    fi
    echo "Command succeeded!"
    

For your script.sh that we're currently working with in the lab, simply running it and seeing "Hello, World" printed to your terminal is how you check if it executed successfully according to the step's requirements.

Did that help clarify how to check the execution status? What kind of status are you looking for specifically?

0 Comments

no data
Be the first to share your comment!