Christopher Negus

Linux Bible


Скачать книгу

to perform actions over and over again until a condition is met or until all data has been processed. One of the most commonly used loops is the for…do loop. It iterates through a list of values, executing the body of the loop for each element in the list. The syntax and a few examples are presented here:

       for VAR in LIST do { body } done

       for NUMBER in 0 1 2 3 4 5 6 7 8 9 do echo The number is $NUMBER done for FILE in `/bin/ls` do echo $FILE done

      You can also write it this way, which is somewhat cleaner:

       for NAME in John Paul Ringo George ; do echo $NAME is my favorite Beatle done

      Each element in the LIST is separated from the next by white space. This can cause trouble if you're not careful because some commands, such as ls -l, output multiple fields per line, each separated by white space. The string done ends the for statement.

      If you're a die-hard C programmer, bash allows you to use C syntax to control your loops:

       LIMIT=10 # Double parentheses, and no $ on LIMIT even though it's a variable! for ((a=1; a <= LIMIT ; a++)) ; do echo "$a" done

      The ″while…do″ and ″until…do″ loops

      Two other possible looping constructs are the while…do loop and the until…do loop. The structure of each is presented here:

       while condition until condition do do { body } { body } done done

      The while statement executes while the condition is true. The until statement executes until the condition is true—in other words, while the condition is false.

      Here is an example of a while loop that outputs the number 0123456789:

       N=0 while [ $N -lt 10 ] ; do echo -n $N let N=$N+1 done

       N=0 until [ $N -eq 10 ] ; do echo -n $N let N=$N+1 done

      Trying some useful text manipulation programs

      Bash is great and has lots of built-in commands, but it usually needs some help to do anything really useful. Some of the most common useful programs you'll see used are grep, cut, tr, awk, and sed. As with all of the best UNIX tools, most of these programs are designed to work with standard input and standard output, so you can easily use them with pipes and shell scripts.

      The general regular expression parser

      The name general regular expression print (grep) sounds intimidating, but grep is just a way to find patterns in files or text. Think of it as a useful search tool. Gaining expertise with regular expressions is quite a challenge, but after you master it, you can accomplish many useful things with just the simplest forms.

      For example, you can display a list of all regular user accounts by using grep to search for all lines that contain the text /home in the /etc/passwd file as follows:

       $ grep /home /etc/passwd

      Or you could find all environment variables that begin with HO using the following command:

       $ env | grep ^HO

      NOTE

      The ^ in the preceding code is the actual caret character, ^, not what you'll commonly see for a backspace, ^H. Type ^, H, and O (the uppercase letter) to see what items start with the uppercase characters HO.

      To find a list of options to use with the grep command, type man grep.

      Remove sections of lines of text (cut)

      The cut command can extract fields from a line of text or from files. It is very useful for parsing system configuration files into easy-to-digest chunks. You can specify the field separator you want to use and the fields you want, or you can break up a line based on bytes.

       $ grep /home /etc/passwd | cut -d':' -f6 - /home/chris /home/joe

      Translate or delete characters (tr)

      The tr command is a character-based translator that can be used to replace one character or set of characters with another or to remove a character from a line of text.

      The following example translates all uppercase letters to lowercase letters and displays the words mixed upper and lower case as a result:

       $ FOO="Mixed UPpEr aNd LoWeR cAsE" $ echo $FOO | tr [A-Z] [a-z] mixed upper and lower case

      In the next example, the tr command is used on a list of filenames to rename any files in that list so that any tabs or spaces (as indicated by the [:blank:] option) contained in a filename are translated into underscores. Try running the following code in a test directory:

       for file in * ; do f=`echo $file | tr [:blank:] [_]` [ "$file" = "$f" ] || mv -i -- "$file" "$f" done

      The stream editor (sed)

      The sed command is a simple scriptable editor, so it can perform only simple edits, such as removing lines that have text matching a certain pattern, replacing one pattern of characters with another, and so on. To get a better idea of how sed scripts work, there's no substitute for the online documentation, but here are some examples of common uses.

      You can use the sed command essentially to do what I did earlier with the grep example: search the /etc/passwd file for the word home. Here the sed command searches the entire /etc/passwd file, searches for the word home, and prints any line containing the word home:

       $ sed -n '/home/p' /etc/passwd chris:x:1000:1000:Chris Negus:/home/chris:/bin/bash joe:x:1001:1001:Joe Smith:/home/joe:/bin/bash

      In this next example, sed searches the file somefile.txt and replaces every instance of the string Mac with Linux. Notice that the letter g is needed at the end of the substitution command to cause every occurrence of Mac on each line to be changed to Linux. (Otherwise, only the first instance of Mac on each line is changed.) The output is then sent to the fixed_file.txt file. The output from sed goes to stdout, so this command redirects the output to a file for safekeeping.

       $ sed 's/Mac/Linux/g'