January 20, 2019

The Unix Shell - Part II - page 2

Introduction,Standard Input and Output.

  • September 2, 1999
  • By Steve Singer

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".


The Unix Shell allows you to string series of simple commands together for complex operations. Each command in the chain takes data, modifies it in some way, and passes it on to the next command. Programs are connected with 'pipes', and as a programmer of shell scripts you get to be the plumber. A pipe takes the output stream of one program and connects it to the input stream of another. For example, pipes make it easy to search many compressed files for lines containing a specific string. Unix systems often have a command named "zcat" that takes a compressed file as an argument and displays the contents of the file in uncompressed form on standard output. To search all of the compressed files in the current directory for the lines containing "hello" we could use the command
"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 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
"echo $FOO".
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.

Most Popular LinuxPlanet Stories