转贴自 http://www.blogjava.net/zjrstar/archive/2008/06/18/208634.html
UNIX Shells by Example(Fourth Edition)读书笔记-1.UNIX/Linux Shells简介
Shell是一些特定的程序接口,用于用户和UNIX/Linux操作系统核交互。如下图所示:
1.Unix Shell
(1) Bourne shell ( sh)是标准的UNIX shell, 用于管理系统. 很多系统管理的脚本。例如 rc start和 stop脚本以及 shutdown 是Bourne shell 脚本;缺省的Bourne shell 表示为( $)
(2) C shell ( csh) 是伯克利开发的,增加了很多新的特征, 例如command-line history, aliasing, built-in arithmetic, filename completion, and job control. Bourne shell 脚本快于和简单于C shell. 缺省C shell 表示为( %)
(3) Korn shell是Bourne shell的超集. Korn shell 增强的特征有editable history, aliases, functions, regular expression wildcards, built-in arithmetic, job control, coprocessing, and special debugging features. Bourne shell is 完全向上兼容Korn shell,q缺省的Korn shell提示符是 ( $)
2.Shell的职责是:
(1)读入输入和解析命令行
(2)计算特殊字符,例如wildcards和历史字符
(3)建立管道,重定向和后台处理
(4)处理信号
(5)建立可执行程序
3.Shell命令执行图:
4.系统启动和登录Shell
启动系统->>调用 init->>分配PID=1->>打开终端线->>建立 stdin, stdout和 stderr->>输入login name,->>输入password->> /bin/login通过 passwd文件验证你的密码.如果 login验证成功,则会开始建立初始环境 HOME, SHELL, USER, 和 LOGNAME 是从 passwd中抽取的变量值. HOME变量指定你的home目录, SHELL指定登录shell的名称,它是passwd文件的最后一个入口. USER 和 LOGNAME 变量指定你的登录名. ->>执行passwd文件中的最后一个入口程序 . 通常这个程序就是shell. 如果 passwd文件中的最后一个程序是 /bin/csh, C shell被执行,如果是 /bin/bash 或者null, Bash shell执行. 如果是 /bin/ksh 或者 /bin/pdksh, Korn shell 被执行. ->>检查系统初始化文件建立->>检查登录目录下的初始化文件. ->>等待用户输入。
4.1解析命令行的过程:
(1)执行历史记录替换
(2)命名行被分割成符合或者字
(3)更新历史记录
(4)处理引用
(5)别名替换和定义函数
(6)建立重定向,后台和管道
(7)执行变量( $user, $name, etc.) 替换
(8)执行命令替换
(9)文件名替换,调用 globbing ( cat abc.??, rm *.c, etc.)
(10)执行命令
4.2命令的类型
(1)别名
(2)关键字
(3)函数
(4)内建命令
(5)可执行命令
5.进程和Shell
5.1查看进程
$ ps aux (BSD/Linux ps) (use ps -ef for SVR4)
$ pstree
6.环境和继承
1 $ id
uid=502(ellie) gid=502(ellie)
查看用户的标识user identification (UID), group identifications (GID),
6.3修改文件许可
chmod
Permission Modes
Decimal | Binary | Permissions |
---|---|---|
0 | 000 | none |
1 | 001 | --x |
2 | 010 | -w- |
3 | 011 | -wx |
4 | 100 | r-- |
5 | 101 | r-x |
6 | 110 | rw- |
7 | 111 | rwx |
1 $ chmod 755 file
$ ls –l file
–rwxr–xr–x 1 ellie 0 Mar 7 12:52 file
2 $ chmod g+w file
$ ls -l file
–rwxrwxr-x 1 ellie 0 Mar 7 12:54 file
3 $ chmod go-rx file
$ ls -l file
–rwx-w---- 1 ellie 0 Mar 7 12:56 file
4 $ chmod a=r file
$ ls -l file
–r--r--r-- 1 ellie 0 Mar 7 12:59 file
chown改变文件和目录的所有属性
1 $ ls -l filetest
-rw-rw-r-- 1 ellie ellie 0 Jan 10 12:19 filetest
6.4工作目录
1 > cd /
2 > pwd
/
3 > bash
4 $ cd /home
5 $ pwd
/home
6 $ exit
7 > pwd
/
>
6.5查看变量
$ env
6.6重定向和管道
重定向
1 $ who > file
2 $ cat file1 file2 >> file3
3 $ mail tom < file
4 $ find / -name file -print 2> errors
5 % ( find / -name file -print > /dev/tty) >& errors
管道
|
who | wc
6.7Shell和信号
标准信号
Number | Name | Description | Action |
---|---|---|---|
0 | EXIT | Shell exits | Termination |
1 | SIGHUP | Terminal has disconnected | Termination |
2 | SIGINT | User presses Ctrl-C | Termination |
3 | SIGQUIT | User presses Ctrl-/ | Termination |
4 | SIGILL | Illegal hardware instruction | Program error |
5 | SIGTRAP | Produced by debugger | Program error |
8 | SIGFPE | Arithmetic error; e.g., division by zero | Program error |
9 | SIGKILL | Cannot be caught or ignored | Termination |
2.1 快速浏览Shell脚本:
C shell 和 TC shell 类似于C语言语法
Bourne shell 是基于Algol语言
Bash和Korn shells 混合了Bourne和C shells, 但是起源于Bourne shell.
2.3 C 和TC Shell 语法结构
shbang行 | "shbang" 行告诉采用哪个shell来解析脚本. 首先是#, !, shell的路径 例如:#!/bin/csh or #!/bin/tcsh | ||||
注释 | 用#符号。例如: # This is a comment | ||||
通配符 | *, ?, and [ ] 用于文件名的扩展. | ||||
输出显示 | echo 输出显示 echo "Hello to you/!" | ||||
局部变量 | 局部变量存在于当前shell中。使用set设置。例如 | ||||
全局变量 | 全局变量被称为环境变量。例如 | ||||
从变量中提取值 | 为了从变量中提取之,使用$在变量前。例如: | ||||
读取用户输入 | 使用 $< 读入一行到一个变量中,例如: | ||||
参数 | 可以从命令行中传入参数。两种方式接受这些参数值:一个是位置参数,另外一个是argv数组。例如: 使用位置参数: | ||||
echo $1 $2 $3 | arg1 指定为$1, arg2 to $2, etc. | ||||
echo $* | 所有的参数 | ||||
使用argv数组: | |||||
echo $argv[1] $argv[2] $argv[3] | |||||
echo $argv[*] | 所有参数 | ||||
echo $#argv | 参数号 | ||||
数组 | 数组是一列被空格分隔的字符。使用()把字符都包含进去。 | ||||
set word_list = ( word1 word2 word3 ) | |||||
set names = ( Tom Dick Harry Fred ) | 从list中删除Tom | ||||
echo $word_list[1] | 显示第一个 | ||||
命令替换 | 使用` `例如: | ||||
set variable_name=`command` echo $variable_name | |||||
set now = `date` |
| ||||
算术 | 使用@来表示计算结果的保存变量。例如 | ||||
操作符 | |||||
相等性: | |||||
== | |||||
!= | |||||
Relational: | |||||
> | greater than | ||||
>= | greater than or equal to | ||||
< | less than | ||||
<= | less than or equal to | ||||
逻辑性: | |||||
&& | and | ||||
|| | or | ||||
! | nSot | ||||
条件语句 | if then. if 必须用endif结尾. 还可以使用if/else。例如: | ||||
The if construct is: if ( expression ) then The if/else construct is: if ( expression ) then | The if/else/else if construct is: if ( expression ) then | ||||
switch 结构: switch variable_name | |||||
循环 | 两种类型循环语句:while和foreach | ||||
文件测试 | 例如: | ||||
–r | 当前用户能读文件 | ||||
–w | 当前用户能写文件 | ||||
–x | 当前用户能执行文件 | ||||
–e | 文件存在 | ||||
–o | 当前用户拥有文件 | ||||
–z | 文件长度为0 | ||||
–d | 文件是目录 | ||||
–f | 文件是普通文件 |
2.3.1 C/TC Shell 脚本
Example 2.2.
1 #!/bin/csh –f
2 # The Party Program––Invitations to friends from the "guest" file
3 set guestfile = ~/shell/guests
4 if ( ! –e "$guestfile" ) then
echo "$guestfile:t non–existent"
exit 1
5 endif
6 setenv PLACE "Sarotini's"
7 @ Time = `date +%H` + 1
8 set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
9 foreach person ( `cat $guestfile` )
10 if ( $person =~ root ) continue
11 mail –v –s "Party" $person << FINIS # Start of here document
Hi $person!Please join me at $PLACE for a party!
Meet me at $Time o'clock. I'll bring the ice cream.
Would you please bring $food[1] and anything else you would like to eat?
Let me know if you can make it. Hope to see you soon.Bourne shell语法和结构:
Your pal,
ellie@`hostname` # or `uname -n`
12 FINIS
13 shift food
14 if ( $#food == 0 ) then
set food = ( cheese crackers shrimp drinks "hot dogs" sandwiches )
endif
15 end
Shbang行 | "shbang" 是脚本起始行,告诉kernel那个shell解析. #!位于行头。#!/bin/sh | |
注释 | 行注释用#符号.例如: # this text is not # interpreted by the shell | |
通配符 | *,?, 和 [ ]用于文件名扩展.例如<, >, 2>, >>, 和 | 用于IO和重定向.为了保证这些符号不被解析,这个字符要被引起来。 例如:文件名扩展:rm *; ls ??; cat file[1-3];引用保护:echo "How are you?" | |
输出显示 | 输出屏幕:echo "What is your name?" | |
局部变量 | 局部变量作用于当前shell,shell结束时局部变量失效.例如variable_name=value name="John Doe" x=5 | |
全局变量 | 全局变量也称为环境变量. 例如: VARIABLE_NAME=value export VARIABLE_NAME PATH=/bin:/usr/bin:. export PATH | |
从变量中提取值 | 使用$.例如: echo $variable_name echo $name echo $PATH | |
读取用户输入 | 使用read。例如: echo "What is your name?" read name read name1 name2 ... | |
参数 (位置参数) | 可以从命令行传入参数。位置参数用于从脚本中接收值。例如: $ scriptname arg1 arg2 arg3 ... | |
脚本中: | ||
echo $1 $2 $3 | 位置参数 | |
echo $* | 所有的位置参数 | |
echo $# | 位置参数号 | |
数组 (位置参数) | Bourne shell不支持数组, 但是词列表可以用位置参数取得. 使用内建set命令来建造列表。用shift移除左边第一个词。位置索引从1开始.例如: | |
set word1 word2 word3 | ||
echo $1 $2 $3 | 显示 word1, word2, and word3 | |
set apples peaches plums | ||
shift | 移走apples | |
echo $1 | 显示列表第一个值 | |
echo $2 | 显示列表第二个值 | |
echo $* | 显示所有值 | |
命名替代 | 为了制定命令的输出或者在字符串中使用命令,使用反引号`.例如:variable_name=`command` echo $variable_name now=`date` echo $now echo "Today is `date`" | |
算术 | Bourne shell不支持算式.要使用命令来完成计算.例如: n=`expr 5 + 5` echo $n | |
操作符 | Bourne shell使用内建test命令操作符来测试数字和字符串.例如: | |
相等: | ||
= | 字符串 | |
!= | 字符串 | |
-eq | 数值 | |
-ne | 数值 | |
逻辑: | ||
-a | And | |
-o | Or | |
! | Not | |
关系的: | ||
-gt | 大于 | |
-ge | 大于等于 | |
-lt | 小于 | |
-le | 小于等于 | |
条件语句 | If结构. 也可以包含在[]中. then用在结尾. If必须用fi结束.例如: | |
The if construct is: | The if/else construct is: | |
if command then block of statements fi if [ expression ] then block of statements fi The if/else/else if construct is: if command then block of statements elif command then block of statements elif command then block of statements else block of statements fi -------------------------- if [ expression ] then block of statements elif [ expression ] then block of statements elif [ expression ] then block of statements else block of statements fi | if [ expression ] then block of statements else block of statements fi The case command construct is: case variable_name in pattern1) statements ;; pattern2) statements ;; pattern3) ;; *) default value ;; esac case "$color" in blue) echo $color is blue ;; green) echo $color is green ;; red|orange) echo $color is red or orange ;; *) echo "Not a color" # default Esac | |
循环 | 3种循环类型: while, until 和 for. 例如: while command do block of statements done while [ expression ] do block of statements done until command for variable in word1 word2 word3 ... do do block of statements block of statements done done until [ expression ] do block of statements done | |
文件测试 | Bourne shell使用内建的测试命令。例如: | |
-d | 文件是目录 | |
-f | 文件存在而且不是目录 | |
–r | 当前的用户读文件 | |
–s | 文件是非0大小 | |
–w | 当前用户可以写文件 | |
–x | 当前用户可以执行文件 | |
Example 2.3. #!/bin/sh 1 if [ –f file ] then echo file exists fi 2 if [ –d file ] then echo file is a directory fi 3 if [ -s file ] then echo file is not of zero length fi 4 if [ -r file -a -w file ] then echo file is readable and writable fi | ||
函数 | 用一个给定的名字定义代码. Bourne shell引入了函数的概念. C 和 TC不容许有函数.例如: function_name() { block of code } ----------------------- lister() { echo Your present working directory is `pwd` echo Your files are: ls } |
Bourne shell 例子:
1 #!/bin/sh2 # The Party Program––Invitations to friends from the "guest" file
3 guestfile=/home/jody/ellie/shell/guests
4 if [ ! –f "$guestfile" ]
5 then
6 echo "`basename $guestfile` non–existent"
7 exit 1
8 fi
9 PLACE="Sarotini's"; export PLACE
10 Time=`date +%H` Time=`expr $Time + 1`
11 set cheese crackers shrimp drinks "hot dogs" sandwiches
12 for person in `cat $guestfile` do
13 if [ $person =~ root ] then
14 continue
15 else
16 # mail –v –s "Party" $person <<- FINIS
17 cat <<-FINIS Hi ${person}! Please join me at $PLACE for a party! Meet me at $Time o'clock. I'll bring the ice cream. Would you please bring $1 and anything else you would like to eat? Let me know if you can make it. Hope to see you soon. Your pal, ellie@`hostname` FINIS 18 shift
19 if [ $# –eq 0 ] then
20 set cheese crackers shrimp drinks "hot dogs" sandwiches fi fi
21 done echo "Bye..."
解释:
1. 让kernel知道是在运行Bourne shell。
2. 注释。
3. 变量guestfile被设置为文件的全路径名,叫做guests.
4. 行读入。
5. then关键字.
6. UNIX basename命名删除所有不再搜索路径里的文件.
7. 如果文件不存在,程序退出。
8. fi关键字表示if结束。
9. 变量指定值place和time. PLACE是环境变量。
10.Time变量值做了命名替换。
11.foods列表指定特定的变量。
12.for循环。
13.如果变量person匹配user root,loop控制将会到达for循环的头,处理下一个person。
14.continue语句是loop控制从12行开始,而不是16行。
15.else语句。
16.
17.next语句,使用cat命令。
18.,循环,移动到下一个人。
19.$#值是位置参数号.如果号码为0,则food列表为空。
20.重置food列表。
21.done关键字表明for循环结束。
Korn Shell
Korn和Bash shells 非常相似.
Korn语法和结构:
Shbang行 | "shbang" 是脚本起始行,告诉kernel那个shell解析. #!位于行头。例如 #!/bin/ksh | |||||
注释 | 行注释用#符号.例如: # This program will test some files | |||||
通配符 | *,?, 和 [ ]用于文件名扩展.例如<, >, 2>, >>, 和 | 用于IO和重定向. 为了保证这些符号不被解析,这个字符要被引起来。 例如:rm *; ls ??; cat file[1-3]; echo "How are you?" | |||||
输出显示 | 输出屏幕echo和print,例如: echo "Who are you?" print "How are you?" | |||||
局部变量 | 局部变量作用于当前shell,shell结束时局部变量失效.例如 variable_name=value typeset variable_name=value name="John Doe" x=5 | |||||
全局变量 | 全局变量也称为环境变量. 例如: export VARIABLE_NAME =value export PATH=/bin:/usr/bin:. | |||||
从变量中提取值 | 使用$.例如: echo $variable_name echo $name echo $PATH | |||||
读取用户输入 | 使用read。例如: | |||||
read name?"What is your name?" | The prompt is in quotes. After it is displayed, the read command waits for user input | |||||
print -n "What is your name?" read name read name1 name2 ... | ||||||
参数 | 可以从命令行传入参数。位置参数用于从脚本中接收值。例如: At the command line: $ scriptname arg1 arg2 arg3 ... In a script: | |||||
echo $1 $2 $ | 位置参数, $1 分配为 arg1, $2 is 分配为arg2, ... | |||||
echo $* | 所有位置参数 | |||||
echo $# | 位置参数号 | |||||
数组 | Bourne shell 利用位置参数创建字符列表.除位置参数外,Korn shell也支持数组语法,起始位置为0. Korn shell数组用set –A命令创建.例如 | |||||
set apples pears peaches | 位置参数 | |||||
print $1 $2 $3 | $1 is apples, $2 is pears, $3 is peaches | |||||
set -A array_name word1 word2 word3 ... set -A fruit apples pears plums | Array | |||||
print ${fruit[0]} | Prints apple | |||||
${fruit[1]} = oranges | Assign a new value | |||||
算术 | Korn shell 支持整数算术.typeset i命令会声明一个整数类型变量. Integer算术能够在变量上完成。否则,(( )) 语法 (let command)用于算术操作。例如: | |||||
typeset -i variable_name | 声明integer | |||||
typeset -i num num=5+4 | num is declared as an integer | |||||
print $num | Prints 9 | |||||
(( n=5 + 5 )) | The let command | |||||
print $n | Prints 10 | |||||
命令替换 | 像C/TC shells 和Bourne shell,Korn shell提供一种新的语法,将命名放在()中,前面加$.例如: variable_name=`command` variable_name=$( command ) echo $variable_name echo "Today is `date`" echo "Today is $(date)" | |||||
操作符 | Korn shell使用内建的test命令操作符,类似于C 语言操作符.例如: | |||||
相等性: | 比较性: | |||||
= | string, equal to | > | greater than | |||
!= | string, not equal to | >= | greater than, equal to | |||
== | number, equal to | < | less than | |||
!= | number, not equal to | <= | less than, equal to | |||
逻辑性: | ||||||
&& | and | |||||
|| | Or | |||||
! | Not | |||||
条件语句 | if 语句条件放在()。then关键字位于()后. If用fi结束. [[ ]] 用于模式匹配. [ ]用于兼容Bourne shell. Case命令是另外一种if/else.例如: | |||||
The if construct is: if command then block of statements fi ---------------------------- if [[ string expression ]] then block of statements fi | ---------------------------- if (( numeric expression )) then block of statements fi | |||||
The if/else construct is: if command then block of statements else block of statements fi -------------------------- if [[ expression ]] then block of statements else block of statements fi --------------------------- if (( numeric expression )) then block of statements else block of statements fi The case construct is: case variable_name in pattern1) statements ;; pattern2) statements ;; pattern3) ;; esac ------------------------- case "$color" in blue) echo $color is blue ;; green) echo $color is green ;; red|orange) echo $color is red or orange ;; esac | The if/else/else if construct is: if command then block of statements elif command then block of statements elif command then block of statements else block of statements fi --------------------------- if [[ string expression ]] then block of statements elif [[ string expression ]] then block of statements elif [[ string expression ]] then block of statements else block of statements fi ---------------------------- if (( numeric expression )) then block of statements elif (( numeric expression )) then block of statements elif (( numeric expression )) then block of statements else block of statements fi | |||||
循环 | 四种类型循环: while, until, for, 和 select. while循环 跟随do。 until循环。 for循环。 select loop is used to provide a prompt (PS3 variable) and a menu of numbered items from which the user inputs a selection The input will be stored in the special built-in REPLY variable. The select loop is normally used with the case command. 循环控制命令,例如: | |||||
while command do block of statements done ---------------------------- while [[ string expression ]] do block of statements done --------------------------- while (( numeric expression )) do block of statements done until command do block of statements done ---------------------------- until [[ string expression ]] do block of statements done ---------------------------- until (( numeric expression )) do block of statements done | for variable in word_list do block of statements done ----------------------------- for name in Tom Dick Harry do print "Hi $name" done select variable in word_list do block of statements done ---------------------------- PS3="Select an item from the menu" for item in blue red green echo $item done Shows menu:
| |||||
文件测试 | Korn shell使用test command来评估表达式,例如: | |||||
-d | File is a directory | |||||
-a | File exists and is not a directory | |||||
–r | Current user can read the file | |||||
–s | File is of nonzero size | |||||
–w | Current user can write to the file | |||||
–x | Current user can execute the file | |||||
Example 2.5. #!/bin/sh 1 if [ –a file ] then echo file exists fi 2 if [ –d file ] then echo file is a directory fi 3 if [ -s file ] then echo file is not of zero length fi 4 if [ -r file -a -w file ] then echo file is readable and writable fi | ||||||
函数 | 函数容许定义一段shell,而且给这段代码给一个名字.有两种格式:一种来自于Bourne shell,另一种来自于Korn shell.例如: function_name() { block of code } function function_name { block of code } ------------------------- function lister { echo Your present working directory is `pwd` echo Your files are: ls } |
Korn Shell脚本:
例子
1 #!/bin/ksh
2 # The Party Program––Invitations to friends from the "guest" file
3 guestfile=~/shell/guests
4 if [[ ! –a "$guestfile" ]]
then
print "${guestfile##*/} non–existent"
exit 1
fi
5 export PLACE="Sarotini's"
6 (( Time=$(date +%H) + 1 ))
7 set -A foods cheese crackers shrimp drinks "hot dogs" sandwiches
8 typeset -i n=0
9 for person in $(< $guestfile)
do
10 if [[ $person = root ]]
then
continue
else
# Start of here document
11 mail –v –s "Party" $person <<- FINIS
Hi ${person}! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring
${foods[$n]} and anythin else you would like to eat? Let
me know if you can make it.
Hope to see you soon.
Your pal,
ellie@`hostname`
FINIS
12 n=n+1
13 if (( ${#foods[*]} == $n ))
then
14 set -A foods cheese crackers shrimp drinks "hot dogs"
sandwiches
fi
fi
15 done
print "Bye..."
解释:
- 让Kernal知道在运行Korn shell script.
- 注释
3. 变量guestfile被设置为文件的全路径名,叫做guests.
- 行读入
- 环境变量.
- the hour of the day指定给变量Time.
- 数组foods赋值,使用 set –A 命令.项开始索引0.
- typeset –i 命令创建integer值.
- For循环.
- 条件测试.
- The mail message is sent. The message body is contained in a here document.
- 变量n增加1.
- 如果数组中的元素号等于变量值,则到达了数据末端.
- 结束循环.
Bash Shell结构
Korn和Bash shells非常相似,但是还是有一些不同之处。Bash的结构如下所示。
Bash Shell语法结构
1 #!/bin/bash
# GNU bash versions 2.x
2 # The Party Program––Invitations to friends from the "guest" file
3 guestfile=~/shell/guests
4 if [[ ! –e "$guestfile" ]]
then
5 printf "${guestfile##*/} non–existent"
exit 1
fi
6 export PLACE="Sarotini's"
7 (( Time=$(date +%H) + 1 ))
8 declare -a foods=(cheese crackers shrimp drinks `"hot dogs"` sandwiches)
9 declare -i n=0
10 for person in $(cat $guestfile)
do
11 if [[ $person == root ]]
then
continue
else
# Start of here document
12 mail –v –s "Party" $person <<- FINIS
Hi $person! Please join me at $PLACE for a party!
Meet me at $Time o'clock.
I'll bring the ice cream. Would you please bring
${foods[$n] and anything else you would like to eat?
Let me know if you can make it.
Hope to see you soon.
Your pal,
ellie@$(hostname)
FINIS
13 n=n+1
14 if (( ${#foods[*]} == $n ))
then
15 declare -a foods=(cheese crackers shrimp drinks `"hot dogs"`
sandwiches)
16 n=0
fi
fi
17 done
printf "Bye..."
解释
- 让kernel知道在运行Bash shell脚本.
- 注释行
- 变量guestfile被设置为文件的全路径名,叫做guests.
- 行读入
- 内建函数printf显示文件名
- 全局环境变量
- 数字表达式
- Bash数组foods定义
- 整数n定于初始值为0
- For循环
- If语句
- 发送mail消息
- 整数n加1
- If语句
- 数组foods重新分配值
- 变量n重新设置回0
- 循环结束
正则表达式元字符
元字符 | 功能 | 例子 | 匹配内容 |
^ | /^love/ | 匹配所有以love开始的行 | |
$ | 行结束锚 | /love$/ | 匹配所有以love结束的行 |
. | 匹配单字符 | /l..e/ | 匹配行中包含l紧跟两个字符然后是e的行 |
* | 匹配0个或者多个前导字符 | / *love/ | 匹配0个或者多个空格,紧跟love |
[ ] | 匹配集合中的一个 | /[Ll]ove/ | 匹配行中包含love 或者Love |
[x–y] | 匹配一个范围集合中的一个字符 | /[A–Z]ove/ | 匹配字符从A-Z紧跟着ove |
[^ ] | 匹配一个不在集合中的字符 | /[^A–Z]/ | 匹配任意字符不在A 和Z之间 |
/ | /love/./ | 匹配包含love,紧跟着. | |
其他的元字符支持 | |||
/< | 单词开始锚 | //<love/ | 匹配行中含有单词以love开始 (supported by vi and grep) |
/> | 单词结束锚 | /love/>/ | 匹配行包含单词以love结束 (supported by vi and grep) |
/(../) | 标签匹配 | //(love/)able /1er/ | 可以使用到9个标签, 起始部分是模式。例如, 模式love被保存为标签1, 被后面引用为/1。在这个例子中,搜索模式包含lovable后紧跟lover (supported by sed, vi, and grep) |
x/{m/} or
x/{m,/} or
x/{m,n/} | 表示字母x出现m次 表示字母x出现至少m次 表示字母x至少出现m次,不超过n次 | o/{5,10/} | 如果一行中含有连续出现5-10次o字母 (supported by vi and grep) |
例子3.1
/love/
模式love可以匹配自己和作为其他词的一部分匹配,例如lovely,gloves和clover。
例子3.2
/^love/
起始锚,匹配只能匹配以love开始的词。
例子3.3
/love$/
结束锚,匹配以love结束的词。
例子3.4
/l.ve/
.只能匹配一个字符。
例子3.5
/o*ve/
*匹配0到多个字符,比如love, loooove, lve
例子3.6
/[Ll]ove/
匹配Love和love
例子3.7
/ove[a-z]/
[a-z]中的任何一个字符都可以匹配
例子3.8
/ove[^a-zA-Z0-9]/
匹配ove后跟的字符不在a-z,A-Z,0-9的字符
例子3.9
/^[A–Z]..$/
匹配起始为[A-Z]之间的字母,跟随两个任意字符,紧接着是一个新行。
/^[A–Z][a–z ]*3[0–5]/
匹配以大写字母开始,跟着0个或者多个小写字母或空格,跟随着数字3和另外一个在0-5之间的数字。
/[a–z]*/ ./
匹配包含0个或多个小写字母,跟着.
/^ *[A–Z][a–z][a–z]$/
匹配其实是0个或者多个空格(tab不能算作空格),跟着大写字母,两个小写字母和一个新行。
/^[A–Za–z]*[^,][A–Za–z]*$/
匹配0个或多个大写或小写字母跟着非逗号,然后紧跟着0个或者多个大写或小写字母结束
例子3.10
//<fourth/>/
在每行上查找fourth这个单词,一个单词可以被空格分隔,结束于标点符号,起始于一行,结束于一行等等。
例子3.11
//<f.*th/>/
匹配单词以f开始,跟着0个或者多个任意的字符,结束语th。
例子3.12
1,$s//([0o]ccur/)ence//1rence/
搜索整个字符串occurrence或者Occurrence
例子3.13
:s//(square/) and /(fair/)//2 and /1/
将会匹配fair and square。
例子3.14
/5/{2/}2/{3/}/./
匹配所有行中包含出现两次数字5,跟着出现三次数字2,跟着.
echo "Bye..."
解释:
1.告诉kernel,这是C shell脚本. ”–f“表示快速启动.就是说不要执行.cshrc.
2.注释行
3.变量guestfile 被设置为调用guests的全路径
4.行读入,如果guests不存在,打印 "guests nonexistent" ,退出脚本。
5.endif
6.PLACE 环境变量
7.Time局部变量。@是内建算术。
8.food数组。
9.foreach循环。命令替换`cat $guestfile`.
10.条件测试
11.foreach循环
12.FINIS是用户定义的终结符
13.shift命令取得下一个person
14.如果food为空,将会重置。
15.循环结束标志