Redirecting errors with exec

By Sandra Henry-Stocker  Add a new comment

It is not at all uncommon for script developers to include sporadic lines ending with "2> /dev/null" in their scripts. These redirects send errors to the bit bucket in order to keep them out of sight for anyone running the script, lest they start to believe they've done something terribly wrong.

When a user needs to be alerted to some problem related to running a script, a more gently crafted message can be displayed in place of the rather coarse and unimaginative output of our Unix binaries. The likes of "/tmp/file109: No such file or directory" and similar errors that only the person who wrote the script is likely to understand can be squelched in favor of "Sorry, but you must supply a valid file name" or "Usage: anaperf ".

To squelch the system's messages and replace them with your own, you can add the redirection to the end of the lines that are likely to generate intimidating messages:

rm * 2>/dev/null
rmdir  2> /dev/null
cp -p *  2> /dev/null

You can then test the return code to see whether an error was generated and supply your own.

if [ $? != 0 ]; then
    echo "Sorry, chum, you don't own that file"
    exit 1
fi

You can also opt to remove all the line-specific "2>/dev/null" add-ons and, instead include a single line near the top of your scipt to send all error output on its way to bit bliss. Just add a line like that shown below and, from that point forward, all standard error disappears.

# don't display any errors
exec 2>/dev/null

You can still generate your own superbly crafted error messages, thus soothing your users' nerves and making them feel good about themselves even if they can't remember that to process a file, they need to supply a file name.

If, instead of tossing away all the errors that are generated, you would like to retain them for whatever reason, you can capture the errors in a file in place of dumping them down the bit bucket with only a slight variation of the command.

# save errors for usage stats
exec 2>/opt/statlog

This exec trick doesn't only work with standard error, of course. You can send standard out to a file with a similar command:

exec > logfile

You can also read data from a file using exec:

exec < datafile
read firstline
read secondline

The exec command can come in handy in a number of interesting ways. The argument you supply to exec is, basically, another command. exec runs the second command without creating a new process. You can think of this as overlaying one process with another. If you were to run the little script shown below, for example, you'd never see the word "hello" being echoed to your screen because the date process would replace the script process.

#!/bin/bash

exec date
echo hello

Similarly, typing a command as innocuous as "exec ls" will close your shell. Why? Because it overlays the shell process with itself.

To replace one shell with another, you might try "exec shell" as in this example:

# ps
   PID TTY         TIME CMD
 27379 pts/3       0:00 ps
 27181 pts/3       0:00 sh
# exec bash
bash-3.00# ps
   PID TTY         TIME CMD
 27380 pts/3       0:00 ps
 27181 pts/3       0:00 bash

While there are many ways to keep system messages from scaring your users, redirects using exec are neat, tidy and about as easy as they come.

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    IT Management/StrategyWhite Papers & Webcasts

    White Paper

    Evaluator Group: Storage Federation - IT Without Limits (Analysis of HP Peer Motion with Storage Federation)

    As the role of IT increases within organizations, the need to move data when and where it is needed is critical to support emerging business requirements. This has become increasingly difficult due to the huge growth of data volumes. This white paper sponsored by HP + Intel evaluates a solution that aims to enable the movement of data without physical limitations. Read now and see how this could enable agility and efficiency.

    White Paper

    ESG Lab Validation Report: HP Data Protector & Deduplication Solutions

    Many organizations have deployed disk-to-disk backup technologies to improve the speed and reliability of their backup and disaster recovery operations. A growing number of these now look to data deduplication to enhance retention periods and reduce costs. This ESG Lab Validation Report sponsored by HP + Intel examines a number of backup and recovery solutions and evaluates their ease of implementation as well as their ability to improve reliability and reduce costs.

    White Paper

    Business Value of Blade

    The nature of the blade platform makes system management, monitoring and provisioning easy and efficient. Access this resource to learn how blade migration will save your data center time and money while increasing performance.

    White Paper

    Accelerate time to application value

    For your IT organization to keep pace with the business, you need a new, faster approach to infrastructure deployment-an approach that increases agility and accelerates time to application value. That's HP Converged Systems. Built on Converged Infrastructure, these systems deliver the industry's first portfolio of pre-integrated, tested, and optimized infrastructure solutions for applications running in virtual, cloud, dedicated, or hybrid environments.

    White Paper

    Converged Infrastructure for Dummies

    As you know, everything is mobile, connected, interactive, and immediate. This is exactly why organizations need a highly agile IT infrastructure in order to keep pace with extreme fluctuations in business demand. This book will help you understand why infrastructure convergence has been widely accepted as the optimal approach for simplifying and accelerating your IT to deliver services at the speed of business while also shifting significantly more IT resources from operations to innovation.

    See more White Papers | Webcasts

    Ask a question

    Ask a Question