Linux Bible. Christopher Negus
Чтение книги онлайн.
Читать онлайн книгу Linux Bible - Christopher Negus страница 71
N=0 while [ $N -lt 10 ] ; do echo -n $N let N=$N+1 done
Another way to output the number 0123456789 is to use an until
loop as follows:
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.
The following example lists all home directories of users on your system. This grep
command line pipes a list of regular users from the /etc/passwd
file and displays the sixth field (-f6
) as delimited by a colon (-d':'
). The hyphen at the end tells cut
to read from standard input (from the pipe).
$ 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' somefile.txt > fixed_file.txt
You can get the same result using a pipe:
$ cat somefile.txt | sed 's/Mac/Linux/g' > fixed_file.txt
By searching for a pattern and replacing it with a null pattern, you delete the original pattern. This example searches the contents of the somefile.txt
file and replaces extra blank spaces at the end of each line (s/ *$
) with nothing (//
). Results go to the fixed_file.txt
file.
$ cat somefile.txt | sed 's/ *$//' > fixed_file.txt
Using simple shell scripts
Sometimes, the simplest of scripts can be the most useful. If you type the same sequence of commands repetitively, it makes sense to store those commands (once!) in a file. The following sections offer a couple of simple, but useful, shell scripts.
Telephone list
This idea has been handed down from generation to generation of old UNIX hacks. It's really quite simple, but it employs several of the concepts just introduced.
#!/bin/bash # (@)/ph # A very simple telephone list # Type "ph new name number" to add to the list, or # just type "ph name" to get a phone number PHONELIST=~/.phonelist.txt # If no command line parameters ($#), there # is a problem, so ask what they're talking about. if [ $# -lt 1 ] ; then echo "Whose phone number did you want? " exit 1 fi # Did you want to add a new phone number? if [ $1 = "new" ] ; then shift echo $*>>