1 / 10

Shell Error Handling

Shell Error Handling. Error Handling . What do you do when your shell script encounters an error? Exit the script Ignore the error and keep on running Assume the user messed up so don't worry about it None of these is particularly attractive, especially for the user of your script.

varian
Download Presentation

Shell Error Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Error Handling

  2. Error Handling • What do you do when your shell script encounters an error? • Exit the script • Ignore the error and keep on running • Assume the user messed up so don't worry about it • None of these is particularly attractive, especially for the user of your script

  3. Handling Error Conditions • There are two major types of errors your scripts should check for and handle • Internal errors within your script • External errors caused by something not associated with your script • If you want to be known as someone who produces high quality robust scripts that are a pleasure to use, then you need to check for these types of errors and respond in an appropriate manner

  4. Internal Errors • Generally caused by: • User not supplying needed information to the script or supplying invalid information • Some command failing to perform as expected • User error conditions are unique and need to be tested for on a case-by-case basis • You need to ensure that the use is not allowed to provide invalid input • You should also allow the user to correct the input if at all possible

  5. To test if a command performed as expected, you need to test the return status of the command • Every time a shell command is executed the shell variable $? is set to the return status the command • Any time you execute a critical command in your script you should test the return status to ensure the command completed successfully • If it did not complete successfully then your script should take some reasonable corrective action

  6. External Errors • Certain actions by the user or the system can cause errors which will interrupt your script, usually at the most inopportune time • These errors are usually captured by the Unix system and communicated to your script via signals • The system is telling your script that something has happened and some action may need to be taken • This action may be any valid shell command or commands or it may be no action at all! • You control how you script will respond by using the trap command

  7. trap • Syntax: • trap "cmd(s)" signal_list • Example • trap "rm /tmp/krf*$$; exit 0" 1 2 3 14 15 • The trap command let's your script decide how to respond to signals passed from the OS • It is particularly useful to allow your script to clean up after itself in case of abnormal termination

  8. Common Signals

  9. Other trap Examples • trap "echo ${filename} > stopfile; exit 0" 1 2 3 14 • stopfile will contain the name of the last file used by your script • trap "" 1 2 3 • Your script will ignore signals 1, 2 and 3, the common ways users attempt to stop a running script • trap "" 1 • Keep running even if the user logs out • trap signal_number resets the trap for signal_number to the default action

  10. By setting a trap for signal number 0, you can specify some action to take whenever the script exits with a 0 status (successful completion) • This can be used for cleanup routines or simply to log a message that the script ran properly • trap "echo "scriptname completed successfully at `date`" >> logfile" 0 • Executing trap with no arguments displays any traps that you have changed

More Related