The Unix Shell - Part II - page 2
Introduction,Standard Input and Output.Redirection
The standard input, output and error streams can be redirected to files. This is useful for saving the output of commands. Redirecting the output of a command can be done by using the 'greater than' symbol ( > ) followed by the name of the file the output is to be saved in.
For example, to save a list of the files in the current directory, use the command
"ls >/tmp/filelist". Running this command will create a file named filelist in
the /tmp directory containing the output of ls. If a file named filelist already exists, it will be overwritten by this command. Appending the
output of a command to an existing file is done with two greater than signs. Our example above would be changed to "ls >>/tmp/filelist". Redirecting the standard error
stream is done similarly. When we pass illegal options to grep we can redirect the errors to a file, as in the following example:
"grep --badoptions 2>/tmp/greperrors".
If you look at /tmp/greperrors you will see the error message which grep would otherwise have displayed on the screen...
Connecting standard input to a file is the reverse process. All of the input the program would normally accept from a keyboard comes from a file. We'll use the grep command again in our example. Running the command
"grep mail </tmp/filelist"
will search the file created in a previous example for lines containing the string "mail".
"zcat *.gzï¿½grep hello".
This command uncompressses all of the files ended with .gz(for gzip) in the current directory and passes the uncompressed data to grep for a search. Grep will then return all lines containing the string "hello". Variables
Variables allow you to store information inside the Shell
Environment. All shell variables
are of the same type and can hold numbers or text. To set a variable in the sh,ksh,bash family of shells (the syntax in csh and tcsh differ) just type
For example, to set the variable FOO, type
Using the data stored in a variable is done by perpending a dollar sign the variable name. For example, to display the contents of our variable we could use the command
Searching the dictionary file for the value stored in FOO can be done with the command
"grep $FOO /usr/dict/words".
The output of a shell command can also be stored in a variable. Enclosing text inside ` symbols tells the shell to treat the text as a command and to execute it. To store the current system time in a variable (for later use) we could use this command:
" STARTTIME=`date` ".
A more complex example might be
" ROOTLINE=`grep root: /etc/passwd` ".
Look at the passwd file and try to guess what $ROOTLINE stores.
Sponsored by BlackBerry
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!