Database Reference
In-Depth Information
Redirection
Echo statements direct their standard output to the screen. This type of output was one of the earliest capabilities for
the Unix operating system and makes log-file creation a simple extension of the scripts. One can redirect output using
either one or two elbows (also referred to as chevrons or angle brackets), typically pointed to the right. A simple echo
statement sends standard output to the screen.
echo "Hello world"
Hello world
You can redirect that same output directly to a file with a right elbow:
echo "Hello world"> myFile.lst
cat myFile.lst
Hello world
Single right elbows create a new file, while double right elbows append output to a file.
echo "Shell scripting makes me hungry">> myFile.lst
echo "What about you?">> myFile.lst
cat myFile.lst
Hello world
Shell scripting makes me hungry
What about you?
Robust shell scripts are written both for unattended scheduled activities (cron, for instance) and for interactive
execution for debugging and one-off use. “Piping to a tee” allows immediate feedback at the console and also writes
standard output to a file, like this:
echo "Hello world"| tee myFile.lst
Hello world
cat myFile.lst
Hello world
Append to files by tacking -a onto your tee statement:
echo "Shell scripting makes me hungry"| tee -a myFile.lst
echo "What about you?"| tee -a myFile.lst
Error Handling
Even the simplest Unix program expects that your program will make the machine do the work. Poorly written scripts
make assumptions based on, and perhaps tested against, existing installations and configurations. As you well know,
nothing stays the same in our business, so program for the unexpected.
 
Search WWH ::




Custom Search