Classic Shell Scripting
Start:2011-03-08
Chapter 3
1、cut command
-c 按字符切割 cut -c 1-2 filename;
-d 按什么作为切割的字符,一般来说配合-f使用;
-f 截取某部分; cut -d: -f 1,2 filename;
2、join command
3、awk command
NF:last line;
-v :sets awk's variables 例如:awk -v 'OFS=chen' '{print $1,$3}' filename 用chen作为打印出来分隔符号
awk 'BEGIN { FS = ":" ; OFS = "**" }; FS读取分隔符;OFS打印分隔符;
Chapter 4 Text Processing Tools
1、sort command
-b : Ignore leading whitespace
-r :Reverse the sort order to descending, rather than the default ascending
-k :Define the sort key field
-u :Unique records only: discard all but the first record in a group with equal keys. Only the key fields matter: other parts of the discarded records may differ
2、uniq command (often used with sort)
sort filename | uniq : Show unique sorted records(过滤重复的)
sort filename | uniq -c : Count unique sorted records(过滤重复的且显示出现次数)
sort filename | uniq -d : Count unique sorted records(获取重复的数据)
sort filename | uniq -u : Show only nonduplicate records(获取不重复的数据)
3、fmt command
功能说明:编排文本文件。
4、wc command
wc -c :Count bytes
wc -l : Count lines
wc -w :Count words
5、pr comman
pr -c5 -t : 指定每行打印几个,并对齐
Chapter 5. Pipelines Can Do Amazing Things
1、Structured Data for the Web
Chapter 6. Variables, Making Decisions, and Repeating Actions
1、export, readonly : export modifies or prints the environment. readonly makes variables unmodifiable.
2、unset command
unset full_name Remove the full_name variable
unset -v first middle last Remove the other variables(many)
Use unset -f to remove functions
3、$# 含义
程序:
- while [ $# != 0 ] $# decremented by shift, loop will terminate
- do
- case $1 in
- ... Process first argument
- esac
- shift Shift first argument away (see later in text)
- done
4、"$*" 所有参数 : Equivalent to "$1 $2”
5、"$@"所有参数 :Equivalent to "$1" "$2"
上述两者区别:
sh test.sh a b运行后
"$*" :The arguments were a b
"$@" :The arguments were a
The arguments were b
6、exit status
0 、successfully
1-125、unsuccessfuly,The meanings of particular exit values are defined by each individual command
126、Command found, but file was not executable.
127、Command not found.
> 128、Command died due to receiving a signal.
7、if-elif-else-fi(选择)
8、There is a difference between using -a and -o, which are test operators, and && and ||, which are shell operators
if [ -n "$str" -a -f "$file" ] Two conditions, one test command
if [ -n "$str" ] && [ -f "$file ] Two commands, short-circuit
if [ -n "$str" && -f "$file" ] Syntax error, see text
注意:For portability, the POSIX standard recommends the use of shell-level tests for multiple conditions, instead of the -a and -o operators. (We also recommend this.)
For this reason, all shell variable expansions should be quoted so that test receives an argument, even if it turns out to be the null string. For example:
if [ -f "$file" ] ... Correct
if [ -f $file ] ... Incorrect
9、cace command
Chapter 7. Input and Output, Files, and Command Evaluation
1、read command
To read information into one or more shell variables.
-r : Raw read. Don't interpret backslash at end-of-line as meaning line continuation
阅读(1411) | 评论(0) | 转发(0) |