(Pipe): Communicates Between ProcessesBecause 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
$
cat months head
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
The
$
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
echo : Displays TextThe 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
The
echo
utility is a good tool for learning about the shell and other Linux programs. Some examples on page 222 use
echo
to
date : Displays the Time and DateThe 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
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;
$ 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
$
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 FormatIf 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. |