Unix® Shell Programming, Third Edition
By Stephen G. Kochan, Patrick Wood
Publisher : Sams Publishing
Pub Date : February 27, 2003
ISBN : 0-672-32490-3
Pages : 456
Slots : 1
1.ls [a-z]*[!0-9]
List files that begin with a lowercase letter and don't end with a digit.
2.ps -f
When
used with the -f option, ps prints out more information about your
processes, including the parent process id (PPID), the time the
processes started (STIME), and the command arguments.
3.Basic Regular Expressions
Matching Any Character: The Period (.) :s/p.o/XXX/g Change all p.os to XXX
Matching
the Beginning of the Line: The Caret (^)
:s/^/>>/ Insert >> at the beginning of each line
Matching the End of the Line: The Dollar Sign ($)
/\.$/ Search for a line that ends with a period
Matching a Choice of Characters: The [...] Construct :s/[aeiouAEIOU]//g Delete all vowels
[0-9] To match any digit
character 0 through 9
[A-Z] To match an uppercase letter, you write
[A-Za-z] To match an upper- or lowercase
letter, you write
Matching a Precise Number of Characters: \{...\} X\{1,10\} matches from one to ten consecutive X's.
[a-zA-Z]\{7\} matches
exactly seven characters
s/[a-zA-Z]\{6,\}/X/g Change words at least 6 letters long to X
4.If a caret (^) appears as the first character after the left bracket, the sense of the match is inverted:
eg. [^A-Z] matches any character except an uppercase letter.
5.:s/[A-Za-z][A-Za-z]*/X/g Matches any words and replace it with X
6.$who | cut -c1-8 Extract the first 8 characters
$who | cut -c5- Extracts characters 5 through the end of the line from each line
$who | cut -c1,13,50 Extract characters 1, 13, and 50 7.How do you
know in advance whether fields are delimited by blanks or tabs?
$sed -n l file If a tab character separates the fields, \t will be displayed instead of the tab.
8. Get together ---- paste
$ls | paste -d' ' -s -
Paste
ls's output, use space as delimiter,the output from ls is piped to
paste, which merges the lines (-s option) from standard input (-),
separating each field with a space (-d' ' option). <=>$echo *
9.sed
General form of the sed command is:sed command file $who | sed 's/ .*$//'
The sed command says to substitute a blank space followed by any
characters up to the end of the line ( .*$) with nothing (//); that is,
delete the characters from the first blank to the end of the line from
each line of the input.
$ sed -n '1,2p' intro Just print the
first 2 lines $ sed -n '/UNIX/p' intro Just print lines containing
UNIX $ sed '1,2d' intro Delete lines 1 and 2 $ sed '/UNIX/d'
intro Delete all lines containing UNIX
10.tr
$ tr '[a-z]' '[A-Z]' < intro To translate all lowercase letters in intro to their uppercase equivalents
$ more /etc/passwd | tr -s ':' '\11
replace one or more ":" with TAB
$ more /etc/passwd |tr -d ':'
delete the ":" in each line
11.grep
The general format of this command is: grep pattern files
$ grep shell ed.cmd Find every occurrence of the word shell in the file ed.cmd
$ grep -v 'UNIX' intro
Print all lines that don't contain UNIX
$ grep -l 'Move_history' *.c
List the files that contain Move_history
$ grep -n 'Move_history' testch.c
Precede matches with line numbers
12.sort
The -u option tells sort to eliminate duplicate lines from the output.
Use the -r option to reverse the order of the sort.
The
-n option to sort specifies that the first field on the line is to be
considered a number, and the data is to be sorted arithmetically.
$ sort +1n data
Skip the first field in the sort
$ sort +2n -t: /etc/passwd
Sort by user id, "-t" to specify the delimited flag with ":"
13.uniq
Generally used with sort:
$ sort names | uniq List duplicate lines:
$ sort names | uniq -d
14.Built-in Integer Arithmetic
The format for arithmetic expansion is $((expression)).
If the variable is not defined or contains a NULL string, its value is assumed to be zero.
$ echo $((i = i + 1)) Equivalent to a = 0 + 1
To
test to see whether i is greater than or equal to 0 and less than or
equal to 100, you can write result=$(( i >= 0 && i <=
100 )) which assigns result 1 if the expression is true and 0 if it's
false.
15.The Single Quote
To keep characters otherwise separated by whitespace characters together. To ignore all enclosed characters,
16.The Double Quote
To ignore most. In particular, the following three characters are not ignored inside double quotes:
Dollar signs $ Back quotes` Backslashes \
If
you want to have the value of a variable substituted, but don't want
the shell to treat the substituted characters specially, you must
enclose the variable inside double quotes.
Double quotes can be used to hide single quotes from the shell, and vice versa:
$ x="' Hello,' he said"
$ echo $x
'Hello,' he said
$ article=' "Keeping the Logins from Lagging," Bell Labs Record'
$ echo $article
"Keeping the Logins from Lagging," Bell Labs Record
17.The Backslash A backslash removes the special meaning of the character that follows
The shell treats a backslash at the end of the line as a line continuation.
$ lines=one'
> 'two Single quotes tell shell to ignore newline
$ echo "$lines"
one
two
$ lines=one\ Try it with a \ instead
> two
$ echo "$lines"
onetwo
If
you put the entire message inside single quotes, the value of x won't
be substituted at the end. If you enclose the entire string in double
quotes, $x will be substituted.
18.The Back Quote
$ echo The date and time is: `date` <=> $ echo The date and time is: $(date)
19.expr
expr
17 \* 6 expr "$file" : ".*" returns the number of characters stored in
the variable file, because the regular expression .* matches all the
characters in the string.
20. Passing Arguments
$* references all the arguments passed to the program.
$# gets set to the number of arguments
Add:
echo "$1 $2" >> phonebook Remove: grep -v "$1"
phonebook > /tmp/phonebook mv /tmp/phonebook phonebook
Directly
access argument 10:${10} shift-----whatever was previously stored
inside $2 will be assigned to $1, The shift command is useful when
processing a variable number of arguments.
Unix® Shell Programming读书笔记1
最新推荐文章于 2024-04-25 09:16:58 发布