Flylib.com

Books Software

 
 
 

(Pipe): Communicates Between Processes


(Pipe): Communicates Between Processes

Because pipes are integral to the functioning of a Linux system, they are introduced here for use in examples. Pipes are covered in detail beginning on page 216.

A process is the execution of a command by Linux (page 300). Communication between processes is one of the hallmarks of both UNIX and Linux. A pipe (written as a vertical bar, , on the command line and appearing as a solid or broken vertical line on keyboards) provides the simplest form of this kind of communication. Simply put, a pipe takes the output of one utility and sends that output as input to another utility. Using UNIX/Linux terminology, a pipe takes standard output of one process and redirects it to become standard input of another process. (For more information refer to "Standard Input and Standard Output" on page 208.) Most of what a process displays on the screen is sent to standard output. If you do not redirect it, this output appears on the screen. Using a pipe, you can redirect the output so that it becomes instead standard input of another utility. For example, a utility such as head can take its input from a file whose name you specify on the command line following the word head , or it can take its input from standard input. Thus, you can give the command shown in Figure 5-5 on page 133 as follows :

$

cat months  head

Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct


The next command displays the number of files in a directory. The wc (word count) utility with the w option displays the number of words in its standard input or in a file you specify on the command line:

$

ls  wc -w

14


You can use a pipe to send output of a program to the printer:

$

tail months  lpr




Four More Utilities

The echo and date utilities are two of the most frequently used members of the large collection of Linux utilities. The script utility records part of a session in a file, and unix2dos makes a copy of a text file that can be read on either a Windows or a Macintosh machine.

echo : Displays Text

The echo utility copies anything you put on the command line after echo to the screen. Some examples appear in Figure 5-9. The last example shows what the shell does with an unquoted asterisk ( * ) on the command line: It expands the asterisk into a list of filenames in the directory.

Figure 5-9. echo copies the command line (but not the word echo ) to the screen
$

ls

memo  memo.0714  practice
$

echo Hi

Hi
$

echo This is a sentence.

This is a sentence.
$

echo star:

*
star: memo memo.0714 practice
$

The echo utility is a good tool for learning about the shell and other Linux programs. Some examples on page 222 use echo to illustrate how special characters , such as the asterisk, work. Throughout Chapters 7, 9, and 28, echo helps explain how shell variables work and how you can send messages from shell scripts to the screen. Refer to the echo info page for more information.

date : Displays the Time and Date

The date utility displays the current date and time:

$

date

Thu Jan 20 10:24:00 PST 2005


The following example shows how you can choose the format and select the contents of the output of date :

$

date +"%A %B %d"

Thursday January 20


Refer to the date info page for more information.

script : Records a Shell Session

The script utility records all or part of a login session, including your input and the system's responses. This utility is useful only from character-based devices, such as a terminal or a terminal emulator. It does capture a session with vim ; however, because vim uses control characters to position the cursor and display different typefaces , such as bold, the output will be difficult to read and may not be useful. When you cat a file that has captured a vim session, the session quickly passes before your eyes.

By default script captures the session in a file named typescript . To use a different filename, follow the script command with a SPACE and the new filename. To append to a file, use the a option after script but before the filename; otherwise script overwrites an existing file. Following is a session being recorded by script :

$

script

Script started, file is typescript
$

date

Thu Jan 20 10:28:56 PST 2005
$

who am i

alex      pts/4    Jan  8 22:15
$
$

apropos mtools

mtools               (1)  - utilities to access DOS disks in Unix
mtools.conf [mtools] (5)  - mtools configuration files
mtoolstest           (1)  - tests and displays the configuration
$

exit

Script done, file is typescript
$


Use the exit command to terminate a script session. You can then view the file you created with cat , less , more , or an editor. Following is the file that was created by the preceding script command:

$

cat typescript

Script started on Thu Jan 20 10:28:56 2005
$ date
Thu Jan 20 10:28:56 PST 2005
$ who am i
alex      pts/4    Jan  8 22:15
$
$ apropos mtools
mtools               (1)  - utilities to access DOS disks in Unix
mtools.conf [mtools] (5)  - mtools configuration files
mtoolstest           (1)  - tests and displays the configuration
$ exit
Script done on Thu Jan 20 10:29:58 2005
$


If you will be editing the file with vim , emacs , or another editor, you can use dos2unix to eliminate from the typescript file the ^M characters that appear at the ends of the lines. Refer to the script man page for more information.

unix2dos : Converts Linux and Macintosh Files to Windows Format

If you want to share a text file that you created on a Linux system with someone on a Windows or Macintosh system, you need to convert the file before the person on the other system can read it easily. The unix2dos utility converts a Linux text file so that it can be read on a Windows or Macintosh system. Give the following command to convert a file named memo.txt (created with a text editor) to a DOS-format file:

$

unix2dos memo.txt


Without any options unix2dos overwrites the original file. You can now email the file as an attachment to someone on a Windows or Macintosh system.

dos2unix


You can use the dos2unix utility to convert Windows or Macintosh files so they can be read on a Linux system:

$

dos2unix memo.txt


See the unix2dos and dos2unix man pages for more information.

You can also use tr to change a Windows or Macintosh text file into a Linux text file. In the following example, the d option causes tr to remove RETURN s (represented by \r ) as it makes a copy of the file:

$

cat memo  tr -d '\r' > memo.txt


The greater than ( > ) symbol redirects the standard output of tr to the file named memo.txt . For more information refer to "Redirecting Standard Output" on page 210. Converting a file the other way without using unix2dos is not as easy.