Bourne Shell Exit Status Examples For Linux and Unix

www.l‮tua‬turi.com
Bourne Shell Exit Status Examples For Linux and Unix

In the Bourne shell (and other shells based on it, such as Bash), the exit status of a command is a number that indicates the success or failure of the command. A exit status of 0 indicates success, while a non-zero exit status indicates failure.

Here are some examples of how to use the exit status in a shell script:

  1. Check the exit status of a command:
#!/bin/sh

# Run a command
command

# Check the exit status of the command
if [ $? -eq 0 ]
then
  echo "Command successful"
else
  echo "Command failed"
fi
  1. Use the exit status of a command to control the flow of a script:
#!/bin/sh

# Run a command
command

# If the command failed, exit the script with a non-zero exit status
if [ $? -ne 0 ]
then
  exit 1
fi

# If the command was successful, continue with the rest of the script
  1. Use the exit status of a command to set the exit status of the script:
#!/bin/sh

# Run a command
command

# Set the exit status of the script to the exit status of the command
exit $?

Keep in mind that these are just a few examples.

Created Time:2017-10-16 14:38:39  Author:lautturi