Three More Utilities


The echo and date utilities are two of the most frequently used from the large collection of Mac OS X utilities. The script utility records part of a session in a file.

echo: Displays Text

The echo utility copies anything you put on the command line after echo to the screen. Some examples are shown in Figure 3-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 3-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 OS X programs. Some examples on page 134 use echo to illustrate how special characters, such as the asterisk, work. Throughout Chapters 8 and 13, echo helps explain how shell variables work and how you can send messages from shell scripts to the screen. Refer to page 720 for more information on echo.

date: Displays the Date and Time

The date utility displays the current date and time:

$ date Mon Mar 13 08:00:38 PST 2006 


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

$ date +"%A %B %d" Monday March 13 


Refer to page 701 for more information on date.

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 filename you want to use. To append material 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, output file is typescript $ date Mon Jan  3 14:26:35 CST 2005 $ who am i alex     ttyp3    Jan  3 14:26 $ apropos msdos fsck_msdos(8)          - DOS/Windows (FAT) file system consistency check mount_msdos(8)          - mount an MS-DOS file system msdos.util(8)           - DOS/Windows (FAT) file system utility newfs_msdos(8)          - construct a new MS-DOS (FAT) file system $ exit exit Script done, output file is typescript $ 


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

$ cat typescript Script started on Mon Jan  3 14:26:34 2005 $ date Mon Jan  3 14:26:35 CST 2005 $ who am i alex     ttyp3    Jan  3 14:26 $ apropos msdos fsck_msdos(8)         - DOS/Windows (FAT) file system consistency check mount_msdos(8)         - mount an MS-DOS file system msdos.util(8)          - DOS/Windows (FAT) file system utility newfs_msdos(8)         - construct a new MS-DOS (FAT) file system $ exit exit Script done on Mon Jan  3 14:28:35 2005 $ 


If you will be editing the file with vim, emacs, or another editor, you can use TR (translate, page 879) as shown in the following command to eliminate from the typescript file the ^M characters that appear at the ends of lines:

$ cat typescript | tr -d '\r' > typescript.good 


The greater than (>) symbol redirects the standard output of tr to the file named transcript.good. For more information refer to "Redirecting Standard Output" on page 122.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net