Main index

Introducing UNIX and Linux


Introduction to shells

Overview
Why do we need a shell?
Shell syntax
      Types of shell command
      Simple commands
      Pipelines
      Grouping commands
      Exit status
      List commands
Arithmetic
      Operators and functions
Making decisions
      The 'test' statement
            Operators used by 'test'
      The 'if' statement
Loops
      'For' loops
      'While' and 'until' loops
Searching for files
      Arguments to 'find'
Formatted output
      Arguments to 'printf'
Passing information to scripts
      Scripts with arguments
      Parameter expansion
Summary
Exercises

Pipelines

A pipeline is a sequence of commands separated by the pipe symbol (|); a single command is also technically a pipeline. We can string any number (subject to system dependent limits) of commands together to form a long pipeline. The following are valid pipelines:

date

This is a simple command that displays the time and date.

who | cut -c1-8 | sort

This is a pipeline of three simple commands; it will list the users currently logged in in alphabetical order, without any of the extra information that who displays. It assumes that usernames are at most 8 characters long. The first command in the pipeline lists users together with more information, including the terminal they are using the system from, and the second - cut - extracts the first eight characters from each line of the output of who. These eight characters are precisely the character columns that contain the usernames. The output of cut is then piped to sort to place the usernames in alphabetical order.

ls -l /usr/local/bin 2> errorfile | wc -l > outputfile

This is a pipeline of two simple commands, each redirecting some of its output, which counts the number of files in /usr/local/bin. If directory /usr/local/bin exists, the number of lines produced by ls -l - and hence the number of files in /usr/local/bin - will be counted by wc, and the result sent to outputfile. If /usr/local/bin does not exist, an error message will be sent to errorfile.

who | VAR=42 mycommand | VAR=99 mycommand

This is a pipeline of three simple commands, the latter two run with variable VAR set to a specific value; since mycommand is not a system utility - it is the name of a script which you will have written - the effect of this pipeline will depend on what you have written in that script.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck