Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19880461
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2008-10-30 16:48:49

§2     命令行编辑

 

2中编辑模式:vi or emacs,初学者建议使用emacs,它更自然。参考书籍:O'Reilly books Learning the vi Editor Learning GNU Emacs。两种模式都可能unix终端的控制键冲突。

 

§2.1  开启命令行编辑

 

Bash默认以emacs模式启动,除非你以-noediting方式启动。切换可以使用$ set -o emacs,或者在.inputrc中设置readline变量。

 

§2.2  历史列表

要修改文件可以从HISTFILE中定义

 

§2.3 emacs编辑模式

见《Linux shell 快捷键》

 

§2.4  vi编辑模式

见《Linux shell 快捷键》

 

 

§2.5  fc命令

       使用c shell风格的历史命令机制,暂不涉及

 

 

§2.6  历史扩展

暂不涉及

 

 

§2.7  readline

主要讲述如何设置命令行快捷键。包含从.inputrc和手工使用bind命令设置

§2.8  键盘习惯(略)

 

§3     环境自定义

本章请参考:《Linux shell - 环境和shell变量》

http://blog.chinaunix.net/u/21908/showart.php?id=1331594

§4     基本的shell程序设计

§4.1 shell脚本和函数

       脚本的执行方式有source 加文件名和直接执行文件名2种。前者是在同一shell执行,后者另外开启一个子shell

 

*函数

 

格式如下:

function functname{

 

    shell commands}

 

or:

functname  ( )

 

{

 

    shell commands }

 

 

删除函数定义:unset -f functname.

查看当前shell的函数定义:declare –f,仅显示函数名:declare –F

 

命令的查找顺序如下:

1.    Aliases

2.    Keywords such as function and several others, like if and for, which we will see in Chapter 5

3.    Functions

4.    Built-ins like cd and type

5.    Scripts and executable programs, for which the shell searches in the directories listed in the PATH environment variable

Shell内置变量builtinenable可以修改这个顺序。详见第7章。使用bash内置命令type可以查看这个顺序。

# type ls

ls is aliased to `ls --color=tty'

# type -a ls

ls is aliased to `ls --color=tty'

ls is /bin/ls

type的其他参数暂略。

§4.2 shell变量

本节请参考:《Linux shell - 环境和shell变量》

 

§4.3 字符串操作符

       字符串操作符的作用如下:

       确保变量存在(定义过且为非空)

       为变量设置默认值

       查找未set变量错误

       移动变量中符合模式匹配的部分值

 

*字符串操作符语法

 

Table 4-1. Substitution operators

Operator

Substitution

${varname:-word}

If varname exists and isn't null, return its value; otherwise return word.

Purpose: Returning a default value if the variable is undefined.

Example: ${count:-0} evaluates to 0 if count is undefined.

${varname:=word}

If varname exists and isn't null, return its value; otherwise set it to word and then return its value. Positional and special parameters cannot be assigned this way.

Purpose: Setting a variable to a default value if it is undefined.

Example: ${count:=0} sets count to 0 if it is undefined.

${varname:?message}

If varname exists and isn't null, return its value; otherwise print varname: followed by message, and abort the current command or script (non-interactive shells only). Omitting message produces the default message parameter null or not set.

Purpose: Catching errors that result from variables being undefined.

Example: {count:?"undefined!"} prints "count: undefined!" and exits if count is undefined.

${varname:+word}

If varname exists and isn't null, return word; otherwise return null.

Purpose: Testing for the existence of a variable.

Example: ${count:+1} returns 1 (which could mean "true") if count is defined.

${varname:offset:length}

Performs substring expansion.[5] It returns the substring of $varname starting at offset and up to length characters. The first character in $varname is position 0. If length is omitted, the substring starts at offset and continues to the end of $varname. If offset is less than 0 then the position is taken from the end of $varname. If varname is @, the length is the number of positional parameters starting at parameter offset.

Purpose: Returning parts of a string (substrings or slices).

Example: If count is set to frogfootman, ${count:4} returns footman. ${count:4:4} returns foot.

 

# sort -nr /etc/passwd | head -${2:-3}

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

# sort -nr /etc/passwd | head -${2-3} 也可以达到同样的效果

 

可以把上述内容写为程序,主要有:添加注释,通过filename=$1增加可读性,添加空行作为分隔。filename=${1:?"filename missing."} 则可以进行更好的错误处理。

*模式和模式匹配

${variable#pattern}  如果pattern匹配variable的开头部分,则去掉variable中开头的pattern部分,最短匹配。

${variable##pattern}     同上,最长匹配

${variable%pattern}      末尾最短匹配

${variable%%pattern}     末尾最长匹配

${variable/pattern/string}  替换,只替换一个匹配

${variable//pattern/string} 替换,替换所有匹配

 

英文注解如下:

The longest match to pattern in variable is replaced by string. In the first form, only the first match is replaced. In the second form, all matches are replaced. If the pattern begins with a #, it must match at the start of the variable. If it begins with a %, it must match with the end of the variable. If string is null, the matches are deleted. If variable is @ or *, the operation is applied to each positional parameter in turn and the expansion is the resultant list.[6]

    比如针对/home/cam/book/long.file.name

Expression                   Result
 
${path##/*/}                      long.file.name
 
${path#/*/}              cam/book/long.file.name
 
$path              /home/cam/book/long.file.name
 
${path%.*}         /home/cam/book/long.file
 
${path%%.*}        /home/cam/book/long

 

文件后缀的替换:outfile=${filename%.pcx}.jpg

提取文件名:bannername=${pathname##*/},使用basename由于要重新开启子shell,效率更低。Dirname${variable%/*}比较也是如此。

更清楚地显示PATH

echo -e ${PATH//:/'\n'}

/usr/kerberos/sbin

/usr/kerberos/bin

/usr/local/sbin

/usr/local/bin

/sbin

/bin

/usr/sbin

/usr/bin

/usr/X11R6/bin

/root/bin

/usr/local/ActiveTcl/bin

/home/meil/program/expect

 

*长度操作符

# echo $TERM

linux

[root@localhost ~]# echo ${#TERM}

5

 

*扩展模式匹配

 

Table 4-3. Pattern-matching operators

Operator

Meaning

*(patternlist)

Matches zero or more occurrences of the given patterns.

+(patternlist)

Matches one or more occurrences of the given patterns.

?(patternlist)

Matches zero or one occurrences of the given patterns.

@(patternlist)

Matches exactly one of the given patterns.

!(patternlist)

Matches anything except one of the given patterns.

 

 

§4.4 命令替换

变量赋值的方法:赋值语句,命令行参数,命令替换。

Bash推荐的格式$(UNIX command),为了兼容Bourne and C shell,也使用` UNIX command`模式。

一些替换的例子:

ls -l $(type -path -all command-name).

给当前在线用户发邮件:mail $(who | cut -d' ' -f1)

6个实例试验不成功,暂略。

§4.5 高级实例

阅读(7040) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~