Four More Utilities

 < Day Day Up > 

The echo and date utilities are two of the most frequently used from the large collection of Linux utilities. The script utility helps you record part of a session in a file, and unix2dos makes a copy of a text file that can be read on a Windows machine.

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 Linux programs. Some examples on page 129 use echo to illustrate how special characters, such as the asterisk, work. Throughout the chapters explaining the shells, echo helps explain how shell variables work and how you can send messages from shell scripts to the screen. Refer to page 647 for more information on echo.

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. Refer to page 630 for more information on date.

 $ date +"%A %B %d" Thursday January 20 

script: Records a Linux 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 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.

unix2dos: Converts Linux Files to Windows Format

If you want to share a text file that you created on a Linux system with someone on a Windows system, you need to convert the file before the person on the Windows system can read it easily. The unix2dos utility converts a Linux text file so that it can be read on a Windows 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 system.

dos2unix

You can use the dos2unix utility to convert DOS 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 (page 804) to change a DOS text file into a Linux text file. In the following example, the d option causes TR to remove RETURNs (represented by \r) as it makes a copy of the file:

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

Converting a file the other way without unix2dos is not as easy.

     < Day Day Up > 


    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213

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