Adding to Your Command Repertoire


Shell scripting is a way of building new, custom-made commands for yourself out of existing Linux tools and commands. Using shell scripts, you can shorten tasks that normally involve many steps into just one stepwhich you design.

Before you begin shell scripting, it will be helpful to become familiar with a few Linux commands that you haven't learned about in previous chapters of this book. Each of these commands can make your life easier when you're shell scripting.

Memorizing this information isn't important. Just become familiar enough with it that you have a general idea about which commands you can use for specific tasks in Linux.

Sending Text to Standard Output with echo

The echo command is simple; this command takes anything you supply as an argument and echoes it by printing it to standard output. echo is one of the commands that appears most often in shell scripts, largely because it enables a shell script to have outputthat is, to provide information to the user. Examples seem almost silly:

 [you@workstation20 you]$ echo "Hello, how are you?" Hello, how are you? [you@workstation20 you]$ 

You'll often use echo to allow commands (scripts) you create to ask questions of the user or to display the results of a calculation or operation. Enclosing the text you want to echo in quotation marks, as shown here, is always a good idea so that the shell doesn't try to interpret special characters, such as the asterisk (*) or question mark (?), for pattern matching.

Performing Simple Calculations with expr

The expr command provides a way of evaluating arithmetic expressions from the command line. Although expr doesn't do floating-point (fractional) math, it is nonetheless a simple, useful command. You supply the numeric and operational parts of the expression as arguments:

 [you@workstation20 you]$ expr 3 + 4 7 [you@workstation20 you]$ expr 144 + 13666 / 10 1510 [you@workstation20 you]$ expr 10 - 10 0 [you@workstation20 you]$  

Be Careful When Using Asterisks

The common operator for multiplication on a computer is the asterisk (*). Recall that at the command prompt, this character also performs filename expansion. Because you want to provide expr with mathematical operators and not a list of matching filenames, always remember to enclose the asterisk in quotation marks when using expr:

 [you@workstation20 you]$ expr 3 '*' 36 108 [you@workstation20 you]$ 


Because each number and operator in the computation is a separate argument, you should always take care to separate the numbers and operators with spaces.

Displaying Text File Beginnings and Endings with head and tail

Sometimes it is useful to be able to display only the first few lines or only the last few lines of a text file. You can use the head and tail commands to accomplish this task. Usually, head and tail are used with one option, a dash followed by the number of lines to display:

 [you@workstation20 you]$ head -4 myfile.txt This is line 1 in a file called myfile.txt that is 16 lines long. This is line 2 in a file called myfile.txt that is 16 lines long. This is line 3 in a file called myfile.txt that is 16 lines long. This is line 4 in a file called myfile.txt that is 16 lines long. [you@workstation20 you]$ tail -2 myfile.txt This is line 15 in a file called myfile.txt that is 16 lines long. This is line 16 in a file called myfile.txt that is 16 lines long. [you@workstation20 you]$ 

For example, suppose you want to find the document class details of a LaTeX file called mylatexfile.tex. Instead of loading the entire file into an editor such as vi or emacs, you could use the head command to display the first line of the file:

 [you@workstation20 you]$ head -1 mylatexfile.tex \documentclass[letterpaper,12pt]{article} [you@workstation20 you]$  

Editing Streams of Data with sed

Sometimes you might want to edit streams of data on-the-fly before outputting them. The command you use to do this is the stream editor, sed, one of the most widely used commands in the Unix world.

The most common way to use sed is to have it accept a stream of data through a pipe (a vertical bar), providing as an argument a command that determines how the data sed receives should be changed. Although there are many sed commands, only one of them, the most common one, is covered in this chapter. It is the s (substitute) command, and it works in either of two ways:

 sed 's/search/replace/' sed 's/search/replace/g' 

In both cases, search is a string of text that is to be searched for, and replace is the text that should replace it. If you append a g to the end of the argument, all occurrences of search are replaced with the text in replace; if you do not use g, only the first occurrence on each line is replaced. Note that nearly any character can be substituted for the slash (/) if necessary (allowing you to use the slash in the search or replace strings when you need to do so). Note that you also can provide multiple commands to sed by separating them with semicolons inside the quotes.

Here are some examples of sed used in combination with head, which provides input to sed through a pipe:

 [you@workstation20 you]$ head -4 myfile.txt | sed 's/file/jolly file/' This is line 1 in a jolly file called myfile.txt that is 16 lines long. This is line 2 in a jolly file called myfile.txt that is 16 lines long. This is line 3 in a jolly file called myfile.txt that is 16 lines long. This is line 4 in a jolly file called myfile.txt that is 16 lines long. [you@workstation20 you]$ head -4 myfile.txt | sed 's/file/silly file/g' This is line 1 in a silly file called mysilly file.txt that is 16 lines long. This is line 2 in a silly file called mysilly file.txt that is 16 lines long. This is line 3 in a silly file called mysilly file.txt that is 16 lines long. This is line 4 in a silly file called mysilly file.txt that is 16 lines long. [you@workstation20 you]$ head -4 myfile.txt | sed 's!line 2!line 2 2/3!' This is line 1 in a file called myfile.txt that is 16 lines long. This is line 2 2/3 in a file called myfile.txt that is 16 lines long. This is line 3 in a file called myfile.txt that is 16 lines long. This is line 4 in a file called myfile.txt that is 16 lines long. [you@workstation20 you]$ head -4 myfile.txt | sed 's/e 2/e 5/;s/e 4/e 7/' This is line 1 in a file called myfile.txt that is 16 lines long. This is line 5 in a file called myfile.txt that is 16 lines long. This is line 3 in a file called myfile.txt that is 16 lines long. This is line 7 in a file called myfile.txt that is 16 lines long. [you@workstation20 you]$ 

In the last example, two commands were used: Data was piped through sed to change the number 2 to the number 5 in the first command (before the semicolon), and the number 4 to the number 7 in the second command (after the semicolon).

For a more real-world example, consider what most people would call a search-and-replace function. Suppose you used LaTeX to write a long novel in which you had named the main character Evanand now you want to change his name to Sebastian. Rather than load the long LaTeX file into a text editor and use the search-and-replace functionality, you could simply use sed and output redirection:

 [you@workstation20 you]$ sed 's/Evan/Sebastian/g' MyNovel.tex > MyNewNovel.tex [you@workstation20 you]$ 

Just like that, you have created a new copy of your novel, MyNewNovel.tex, in which the main character's name appears as Sebastianno additional work needed.

Matching the Beginnings or Endings of Lines

You can use the dollar sign ($) and caret (^) in a sed command to match the end or beginning of a line, respectively. For example, to substitute the text goodbye with farewall only when goodbye occurs at the end of a line, use the following sed command:

 's/goodbye$/farewell/' 

Similarly, to substitute the text Hello with Greetings only when Hello occurs at the beginning of a line, use the following sed command:

 's/^Hello/Greetings/' 

The sed command is a staple in the Unix world because it is much more powerful than the few examples here have demonstrated. sed is especially powerful when its pattern-matching capabilities are used.

To learn more about sed, take time to study the sed manual page at your leisure; it will likely prove helpful to you as you use the shell on an ongoing basis.




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

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