Chinaunix首页 | 论坛 | 博客
  • 博客访问: 547202
  • 博文数量: 84
  • 博客积分: 1529
  • 博客等级: 上尉
  • 技术积分: 1472
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 17:57







分类: Python/Ruby

2011-03-09 17:55:21

Classic Shell Scripting
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、$# 含义
  1. while [ $# != 0 ] $# decremented by shift, loop will terminate
  2. do
  3. case $1 in
  4. ... Process first argument
  5. esac
  6. shift Shift first argument away (see later in text)
  7. done

4、"$*" 所有参数 : Equivalent to "$1 $2”
5、"$@"所有参数 :Equivalent to "$1" "$2" 
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.
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
阅读(1259) | 评论(0) | 转发(0) |