Piping & redirecting.


As I mentioned in the File System section, the ability to "connect" several commands together, called piping, or to redirect a command's result is important & also very useful. Through this method it is possible to carry out many commands in one process, & to create accurate file logging, amongst many others.

Piping is the more common of the two, & is simply a case of "piping" the result of one command through another command. The pipe command is | or "[ALT GR] + [<>|]" & is always space separated from the other commands. A simple example; ls -ahl | less will list the contents of the directory one screen at a time.

ifconfig `ppp0 | grep inet | cut -d: -f2 | cut -d " " -f1`

Is another good example of multiple piping, try typing it & see what happens. If you don't have ADSL then you will have to exchange "ppp0" for the device that you use to get your connection to the internet. These 4 commands (ifconfig, grep, & cut) will tell you your IP address, your IP that your ISP (Internet Service Provider) gave you when you connected to them. There are certainly many other ways of finding out your IP, but for the web server it's important for almost all of the automatic functions to know what it's IP is. The above example will naturally give me my IP address, but it will also display a result when I don't have an IP, i.e. when the server is off-line which is also very important. More to this in greater detail in the "detailed web server section". When redirecting a command or a process it's very important to know which part information you wish to have redirected.. Every process has 3 basic states, the standard input (stdin) in most cases the keyboard, standard output (stdout) normally associated with the computer monitor, & standard error (stderr). Each of these states has a number assigned to it:

stdin : 0
stdout : 1
stderr : 2

which are used when defining which of the states you can use. Here's an example of the use of this type of process state. It is used as part of a crontab job (see scripts & crontab) which runs a small script to check the connection to the internet. The script is executed every 5 minutes & a log file is maintained using the stdout (1) & the stderr (2) states.

0,5,10,15,20,25,30,35,40,45,50,55, * * * * . /usr/local/auto.scripts/line-check >> /usr/local/auto.scripts/check.log 2>&1

There are several ways of redirecting the input/output from a particular process. In the above example you see that between the actual script that is being executed (line-check) & the log file (check.log) there are 2 ">>" characters. They define that the multiple output of the script, either the stderr or the stdout, should go to the log file.

In the example below there is only one output, the results that would be displayed on the computer monitor, therefore we only use one > for the redirection.

ls -R /home/user > /home/user/list.dat

The general use of this type of redirection of the process states is particularly useful for logging purposes.



back a page    back to main index    forward a page
copyright 2001 Rob Hawke.
rob@highasakite.net