welcome to my blog
C和C++中如何互相调用(#ifdef __cplusplus)
shell精华集锦【转】
正则表达式基础
|
| 1.1 句点符号 |
| 假 设你在玩英文拼字游戏,想要找出三个字母的单词,而且这些单词必须以“t”字母开头,以“n”字母结束。另外,假设有一本英文字典,你可以用正则表达式搜 索它的全部内容。要构造出这个正则表达式,你可以使用一个通配符——句点符号“.”。这样,完整的表达式就是“t.n”,它匹配“tan”、“ten”、 “tin”和“ton”,还匹配“t#n”、“tpn”甚至“t n”,还有其他许多无意义的组合。这是因为句点符号匹配所有字符,包括空格、Tab字符甚至换行符: |
|
| 1.2 方括号符号 |
| 为 了解决句点符号匹配范围过于广泛这一问题,你可以在方括号(“[]”)里面指定看来有意义的字符。此时,只有方括号里面指定的字符才参与匹配。也就是说, 正则表达式“t[aeio]n”只匹配“tan”、“Ten”、“tin”和“ton”。但“Toon”不匹配,因为在方括号之内你只能匹配单个字符: |
![]() |
| 1.3 “或”符号 |
| 如 果除了上面匹配的所有单词之外,你还想要匹配“toon”,那么,你可以使用“|”操作符。“|”操作符的基本意义就是“或”运算。要匹配“toon”, 使用“t(a|e|i|o|oo)n”正则表达式。这里不能使用方扩号,因为方括号只允许匹配单个字符;这里必须使用圆括号“()”。圆括号还可以用来分 组,具体请参见后面介绍。 |
|
| 1.4 表示匹配次数的符号 |
| 表一显示了表示匹配次数的符号,这些符号用来确定紧靠该符号左边的符号出现的次数: |
![]() |
| 假 设我们要在文本文件中搜索美国的社会安全号码。这个号码的格式是999-99-9999。用来匹配它的正则表达式如图一所示。在正则表达式中,连字符 (“-”)有着特殊的意义,它表示一个范围,比如从0到9。因此,匹配社会安全号码中的连字符号时,它的前面要加上一个转义字符“\”。 |
|
图一:匹配所有123-12-1234形式的社会安全号码 |
| 假设进行搜索的时候,你希望连字符号可以出现,也可以不出现——即,999-99-9999和999999999都属于正确的格式。这时,你可以在连字符号后面加上“?”数量限定符号,如图二所示: |
|
图二:匹配所有123-12-1234和123121234形式的社会安全号码 |
| 下面我们再来看另外一个例子。美国汽车牌照的一种格式是四个数字加上二个字母。它的正则表达式前面是数字部分“[0-9]{4}”,再加上字母部分“[A-Z]{2}”。图三显示了完整的正则表达式。 |
|
图三:匹配典型的美国汽车牌照号码,如8836KV |
| 1.5 “否”符号 |
| “^”符号称为“否”符号。如果用在方括号内,“^”表示不想要匹配的字符。例如,图四的正则表达式匹配所有单词,但以“X”字母开头的单词除外。 |
|
图四:匹配所有单词,但“X”开头的除外 |
| 1.6 圆括号和空白符号 |
| 假设要从格式为“June 26, 1951”的生日日期中提取出月份部分,用来匹配该日期的正则表达式可以如图五所示: |
|
图五:匹配所有Moth DD,YYYY格式的日期 |
| 新出现的“\s”符号是空白符号,匹配所有的空白字符,包括Tab字符。如果字符串正确匹配,接下来如何提取出月份部分呢?只需在月份周围加上一个圆括号创建一个组,然后用ORO API(本文后面详细讨论)提取出它的值。修改后的正则表达式如图六所示: |
|
图六:匹配所有Month DD,YYYY格式的日期,定义月份值为第一个组 |
| 1.7 其它符号 |
| 为简便起见,你可以使用一些为常见正则表达式创建的快捷符号。如表二所示: |
| 表二:常用符号 |
|
| 例如,在前面社会安全号码的例子中,所有出现“[0-9]”的地方我们都可以使用“\d”。修改后的正则表达式如图七所示: |
|
三个UNIX文件时间ctime,mtime,atime(转)
Linux中文件查找技术大全
ermissiondenied."(禁止访问)字样。系统将无法查询到你想要的文件。为了避免这样的错误,我们可是使用转移错误提示的方法尝试着查找文件,输入
用FUSE开发自己的文件系统二 [转]
|
|
|
|
|
|
|
|
Listing 4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
用FUSE开发自己的文件系统一 [转]
|
|
|
|
|
如何将整型数转成字符型?
Unix 高级用户命令 lsof
Overview
LiSt Open Files is a useful and powerful tool that will show you opened files. In Unix everything is a file: pipes are files, IP sockets are files, unix sockets are files, directories are files, devices are files, inodes are files...
Usful Examples
So in this tangle of files lsof listst files opened by processes running on your system.
When lsof is called without parameters, it will show all the files opened by any processes.
lsof | nl
Let us know who is using the apache executable file, /etc/passwd, what files are opened on device /dev/hda6 or who's accessing /dev/cdrom:
lsof `which apache2`
lsof /etc/passwd
lsof /dev/hda6
lsof /dev/cdromNow show us what process IDs are using the apache binary, and only the PID:
lsof -t `which apache2`
Show us what files are opened by processes whose names starts by "k" (klogd, kswapd...) and bash. Show us what files are opened by init:
lsof -c k
lsof -c bash
lsof -c initShow us what files are opened by processes whose names starts by "courier", but exclude those whose owner is the user "zahn":
lsof -c courier -u ^zahn
Show us the processes opened by user apache and user zahn:
lsof -u apache,zahn
Show us what files are using the process whose PID is 30297:
lsof +p 30297
Search for all opened instances of directory /tmp and all the files and directories it contains:
lsof +D /tmp
List all opened internet sockets and sockets related to port 80:
lsof -i
lsof -i :80List all opened Internet and UNIX domain files:
lsof -i -U
Show us what process(es) has an UDP connection opened to or from the host www.akadia.com at port 123 (ntp):
lsof -iUDP@www.akadia.com:123
lsof provides many more options and could be an unvaluable foresinc tool if your system get compromised or as daily basis check tool.
Bourne Shell (一) [转]
Bourne Shell
介绍:Bourne Shell 基础及其他很多有用的特性,shell编程及组织。
主要内容:
.shell基础 基本介绍,环境,选项,特殊字符
.shell变量 用户定义变量,环境变量,位置变量(shell 参数)
.shell script编程
条件测试,循环及重复控制
.shell定制
1.shell基础知识
作者:Stephen Bourne 在Bell实验室开发
建议:man sh 查看相关UNIX上的改进或特性
(1)shell提示符及其环境
/etc/passwd文件
提示符:$
/etc/profile $HOME/.profile
(2)shell执行选项
-n 测试shell script语法结构,只读取shell script但不执行
-x 进入跟踪方式,显示所执行的每一条命令,用于调度
-a Tag all variables for export
-c "string" 从strings中读取命令
-e 非交互方式
-f 关闭shell文件名产生功能
-h locate and remember functions as defind
-i 交互方式
-k 从环境变量中读取命令的参数
-r 限制方式
-s 从标准输入读取命令
-t 执行命令后退出(shell exits)
-u 在替换中如使用未定义变量为错误
-v verbose,显示shell输入行
这些选项可以联合使用,但有些显然相互冲突,如-e和-i.
(3)受限制shell(Restircted Shell)
sh -r 或 /bin/rsh
不能执行如下操作:cd, 更改PATH,指定全路径名,输出重定向,因此可以提供一个较
好的控制和安全机制。通常rsh用于应用型用户及拨号用户,这些用户通常是看不到提
示符的。通常受限制用户的主目录是不可写的。
不足:如果用户可以调用sh,则rsh的限制将不在起作用,事实上如果用户在vi及more
程序中调用shell,而这时rsh的限制将不再起作用。
(4)用set改变 shell选项
用户可以在$提示符下用set命令来设置或取消shell的选项。使用-设置选项,+取消相应
选项,大多数UNIX系统允许a,e,f,h,k,n,u,v和x的开关设置/取消。
set -xv
启动跟踪方式;显示所有的命令及替换,同样显示输入。
set -tu
关闭在替换时对未定义变量的检查。
使用echo $-显示所有已设置的shell选项。
(5)用户启动文件 .profile
PATH=$PATH:/usr/loacl/bin; export PATH
(6)shell环境变量
CDPATH 用于cd命令的查找路径
HOME /etc/passwd文件中列出的用户主目录
IFS Internal Field Separator,默认为空格,tab及换行符
MAIL /var/mail/$USERNAME mail等程序使用
PATH
PS1,PS2 默认提示符($)及换行提示符(>)
TERM 终端类型,常用的有vt100,ansi,vt200,xterm等
示例:$PS1="test:";export PS1
test: PS1="$";export PS1
$echo $MAIL
/var/mail/username
(7)保留字符及其含义
$ shell变量名的开始,如$var
| 管道,将标准输出转到下一个命令的标准输入
# 注释开始
& 在后台执行一个进程
? 匹配一个字符
* 匹配0到多个字符(与DOS不同,可在文件名中间使用,并且含.)
$- 使用set及执行时传递给shell的标志位
$! 最后一个子进程的进程号
$# 传递给shell script的参数个数
$* 传递给shell script的参数
$@ 所有参数,个别的用双引号括起来
$? 上一个命令的返回代码
Borne Shell
介绍:Bourne Shell 基础及其他很多有用的特性,shell编程及组织。
主要内容:
.shell基础 基本介绍,环境,选项,特殊字符
.shell变量 用户定义变量,环境变量,位置变量(shell 参数)
.shell script编程
条件测试,循环及重复控制
.shell定制
1.shell基础知识
作者:Stephen Bourne 在Bell实验室开发
建议:man sh 查看相关UNIX上的改进或特性
(1)shell提示符及其环境
/etc/passwd文件
提示符:$
/etc/profile $HOME/.profile
(2)shell执行选项
-n 测试shell script语法结构,只读取shell script但不执行
-x 进入跟踪方式,显示所执行的每一条命令,用于调度
-a Tag all variables for export
-c "string" 从strings中读取命令
-e 非交互方式
-f 关闭shell文件名产生功能
-h locate and remember functions as defind
-i 交互方式
-k 从环境变量中读取命令的参数
-r 限制方式
-s 从标准输入读取命令
-t 执行命令后退出(shell exits)
-u 在替换中如使用未定义变量为错误
-v verbose,显示shell输入行
这些选项可以联合使用,但有些显然相互冲突,如-e和-i.
(3)受限制shell(Restircted Shell)
sh -r 或 /bin/rsh
不能执行如下操作:cd, 更改PATH,指定全路径名,输出重定向,因此可以提供一个较
好的控制和安全机制。通常rsh用于应用型用户及拨号用户,这些用户通常是看不到提
示符的。通常受限制用户的主目录是不可写的。
不足:如果用户可以调用sh,则rsh的限制将不在起作用,事实上如果用户在vi及more
程序中调用shell,而这时rsh的限制将不再起作用。
(4)用set改变 shell选项
用户可以在$提示符下用set命令来设置或取消shell的选项。使用-设置选项,+取消相应
选项,大多数UNIX系统允许a,e,f,h,k,n,u,v和x的开关设置/取消。
set -xv
启动跟踪方式;显示所有的命令及替换,同样显示输入。
set -tu
关闭在替换时对未定义变量的检查。
使用echo $-显示所有已设置的shell选项。
(5)用户启动文件 .profile
PATH=$PATH:/usr/loacl/bin; export PATH
(6)shell环境变量
CDPATH 用于cd命令的查找路径
HOME /etc/passwd文件中列出的用户主目录
IFS Internal Field Separator,默认为空格,tab及换行符
MAIL /var/mail/$USERNAME mail等程序使用
PATH
PS1,PS2 默认提示符($)及换行提示符(>)
TERM 终端类型,常用的有vt100,ansi,vt200,xterm等
示例:$PS1="test:";export PS1
test: PS1="$";export PS1
$echo $MAIL
/var/mail/username
(7)保留字符及其含义
$ shell变量名的开始,如$var
| 管道,将标准输出转到下一个命令的标准输入
# 注释开始
& 在后台执行一个进程
? 匹配一个字符
* 匹配0到多个字符(与DOS不同,可在文件名中间使用,并且含.)
$- 使用set及执行时传递给shell的标志位
$! 最后一个子进程的进程号
$# 传递给shell script的参数个数
$* 传递给shell script的参数
$@ 所有参数,个别的用双引号括起来
$? 上一个命令的返回代码
当前shell的名字
$n (n:1-) 位置参数
$$ 进程标识号(Process Identifier Number, PID)
>file 输出重定向
`command` 命令替换,如 filename=`basename /usr/local/bin/tcsh`
>>fiile 输出重定向,append
转义符及单引号:
$echo "$HOME $PATH"
/home/hbwork /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:
$echo '$HOME $PATH'
$HOME $PATH
$echo $HOME $PATH
$HOME /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hbwork/bin
其他:
$dir=ls
$$dir
$alias dir ls
$dir
ls > filelist
ls >> filelist
wc -l < filelist
wc -l filelist
sleep 5; echo 5 seconds reaches; ls -l
ps ax |egrep inetd
find / -name core -exec rm {} \; &
filename=`date "+%Y%m%d"`.log
2. shell变量
变量:代表某些值的符号,如$HOME,cd命令查找$HOME,在计算机语言中可以使用变量可以
进行多种运算和控制。
Bourne Shell有如下四种变量:
.用户自定义变量
.位置变量即 shell script之参数
.预定义变量(特殊变量)
.环境变量(参考shell定制部分)
(1)用户自定义变量(数据的存储)
$ COUNT=1
$ NAME="He Binwu"
技巧:因为大部分UNIX命令使用小写字符,因此在shell编程中通常使用全大写变量,
当然这并不是强制性的,但使用大写字符可以在编程中方便地识别变量。
变量的调用:在变量前加$
$ echo $HOME
/home/hbwork
$ WEEK=Satur
$ echo Today is $WEEKday
Today is
$echo Today is ${WEEK}day
Today is Saturday
Shell变量赋值从右从左进行(Linux Shell/bash从左向右赋值!)
$ X=$Y Y=y
$ echo $X
y
$ Z=z Y=$Z
$ echo $Y
$
使用unset命令删除变量的赋值
$ Z=hello
$ echo $Z
hello
$ unset Z
$ echo $Z
$
有条件的命令替换
在Bourne Shell中可以使变量替换在特定条件下执行,即有条件的环境变量替换。
这种变量替换总是用大括号括起来的。
.设置变量的默认值
在变量未赋值之前其值为空。Bourne Shell允许对变量设置默认值,其格式如下:
${variable:-defaultvalue}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:-there}
Hello there
$ echo $UNAME #变量值并未发生变化
$ UNAME=hbwork
$ echo Hello ${UNAME:-there}
Hello hbwork
$
.另一种情况:改变变量的值,格式如下:
${variable:=value}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:=there}
Hello there
$ echo $UNAME #变量值并未发生变化
there
$
.变量替换中使用命令替换
$USERDIR=${$MYDIR:-`pwd`}
.在变量已赋值时进行替换 ${variable:+value}
.带有错误检查的有条件变量替换
${variable:?value}
例:
$ UNAME=
$ echo ${UNAME:?"UNAME has not been set"}
UNAME: UNAME has not been set
$ echo ${UNAME:?}
UNAME: parameter null or not set
(2)位置变量(Shell参数)
在shell script中位置参数可用..表示,表示内容通常为当前执行程序的文件名。
.防止变量值被替换 readonly variable
.使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shell
将为其设置一个新的环境让其执行,这称之了subshell. 在Bourne Shell中变量通常
被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。使
用export对subshell可用。
例:对变量PS1的export操作,shell的提示符将发生变化。
$ PS1=`hostname`$
peony$sh
$ echo $PS1
$ <-输出结果
$ exit
peony$export PS1
peony$sh
peony$ echo $PS1
peony$ <-输出结果
peony$
3.Shell Script编程
目的:使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。
(1)最简单的Shell 编程
$ls -R / |grep myname |more
每天数据的备份:
$ cd /usr/yourname; ls * |cpio -o > /dev/rmt/0h
书写程序的目的是一次编程,多次使用(执行)!
$ cat > backup.sh
cd /home/hbwork
ls * | cpio -o > /dev/rmt/0h
^D
执行:
$ sh backup.sh
或:
$ chmod +x backup.sh
$ ./backup.sh
技巧:在shell script中加入必要的注释,以便以后阅读及维护。
(2)shell是一个(编程)语言
同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shell script
编程更为有用,如:数据变量、参数传递、判断、流程控制、数据输入和输出,子
程序及以中断处理等。
. 在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中:
cd $WORKDIR
ls * | cpio -o > /dev/rmt/0h
. Shell编程中的注释以#开头
. 对shell变量进行数字运算,使用expr命令
expr integer operator integer
其中operator为+ - * / %, 但对*的使用要用转义符\,如:
$expr 4 \* 5
20
$int=`expr 5 + 7`
$echo $int
12
(3)Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read)
restoreall.sh 对backup.sh程序的备份磁带进行恢复
$cat > restoreall.sh
cd $WORKDIR
cpio -i < /dev/rmt/0h
^D
restore1.sh:只能恢复一个文件
#restore1 --program to restore a single file
cd $WORKDIR
cpio -i $ < /dev/rmt/0h
$restore1 file1
恢复多个文件restoreany :
#restoreany --program to restore a single file
cd $WORKDIR
cpio -i $* < /dev/rmt/0h
$ restoreany file1 file2 file3
(4)条件判断
. if-then语句,格式如下:
if command_1
then
command_2
command_3
fi
command_4
在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和
command_3,而command_4总是执行.
示例程序unload: 在备份成功时删除原始文件,带有错误检查
cd
#备份时未考虑不成功的情况!
ls -a | cpio -o > /dev/rmt/0h
rm -rf *
改进如下:
#当使用了管道命令时,管理命令的返回代码为最后一个命令的返回代码
if ls -a | cpio -o > /dev/rmt/0h
then
rm -rf *
fi
. if-then-else语句
if command_1
then
command_2
else
command_3
fi
技巧: 由于shell对命令中的多余的空格不作任何处理,一个好的程序员会用这一特性
对自己的程序采用统一的缩进格式,以增强自己程序的可读性.
. 使用test命令进行进行条件测试
格式: test conditions
test在以下四种情况下使用: a. 字符比较 b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常民政部下均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号将整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
例: 重写unload程序,使用test测试
#!/bin/sh
#unload - program to backup and remove files
#syntax: unload directory
#check arguments
if [ $# -ne 1 ]
then
echo "usage: directory"
exit 1
fi
#check for valid directory name
if [ ! -d "" ]
then
echo " is not a directory"
exit 2
fi
cd
ls -a | cpio -o > /dev/rmt/0h
if [ $? -eq 0 ]
then
rm -rf *
else
echo "A problem has occured in creating backup"
echo "The directory will not be ereased"
echo "Please check the backup device"
exit 3
fi
# end of unload
在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二是
设置程序的退出状态
g. if嵌套及elif结构
if command
then
command
else
if command
then
command
else
if command
then
command
fi
fi
fi
改进:使用elif结构
if command
then
command
elif command
then
command
elif command
then
command
fi
elif结构同if结构类似,但结构更清淅,其执行结果完全相同.
-------------未完,待续----------
当前shell的名字
$n (n:1-) 位置参数
$$ 进程标识号(Process Identifier Number, PID)
>file 输出重定向
`command` 命令替换,如 filename=`basename /usr/local/bin/tcsh`
>>fiile 输出重定向,append
转义符及单引号:
$echo "$HOME $PATH"
/home/hbwork /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:
$echo '$HOME $PATH'
$HOME $PATH
$echo $HOME $PATH
$HOME /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hbwork/bin
其他:
$dir=ls
$$dir
$alias dir ls
$dir
ls > filelist
ls >> filelist
wc -l < filelist
wc -l filelist
sleep 5; echo 5 seconds reaches; ls -l
ps ax |egrep inetd
find / -name core -exec rm {} \; &
filename=`date "+%Y%m%d"`.log
2. shell变量
变量:代表某些值的符号,如$HOME,cd命令查找$HOME,在计算机语言中可以使用变量可以
进行多种运算和控制。
Bourne Shell有如下四种变量:
.用户自定义变量
.位置变量即 shell script之参数
.预定义变量(特殊变量)
.环境变量(参考shell定制部分)
(1)用户自定义变量(数据的存储)
$ COUNT=1
$ NAME="He Binwu"
技巧:因为大部分UNIX命令使用小写字符,因此在shell编程中通常使用全大写变量,
当然这并不是强制性的,但使用大写字符可以在编程中方便地识别变量。
变量的调用:在变量前加$
$ echo $HOME
/home/hbwork
$ WEEK=Satur
$ echo Today is $WEEKday
Today is
$echo Today is ${WEEK}day
Today is Saturday
Shell变量赋值从右从左进行(Linux Shell/bash从左向右赋值!)
$ X=$Y Y=y
$ echo $X
y
$ Z=z Y=$Z
$ echo $Y
$
使用unset命令删除变量的赋值
$ Z=hello
$ echo $Z
hello
$ unset Z
$ echo $Z
$
有条件的命令替换
在Bourne Shell中可以使变量替换在特定条件下执行,即有条件的环境变量替换。
这种变量替换总是用大括号括起来的。
.设置变量的默认值
在变量未赋值之前其值为空。Bourne Shell允许对变量设置默认值,其格式如下:
${variable:-defaultvalue}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:-there}
Hello there
$ echo $UNAME #变量值并未发生变化
$ UNAME=hbwork
$ echo Hello ${UNAME:-there}
Hello hbwork
$
.另一种情况:改变变量的值,格式如下:
${variable:=value}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:=there}
Hello there
$ echo $UNAME #变量值并未发生变化
there
$
.变量替换中使用命令替换
$USERDIR=${$MYDIR:-`pwd`}
.在变量已赋值时进行替换 ${variable:+value}
.带有错误检查的有条件变量替换
${variable:?value}
例:
$ UNAME=
$ echo ${UNAME:?"UNAME has not been set"}
UNAME: UNAME has not been set
$ echo ${UNAME:?}
UNAME: parameter null or not set
在shell script中位置参数可用..表示,
Bourne Shell
介绍:Bourne Shell 基础及其他很多有用的特性,shell编程及组织。
主要内容:
.shell基础 基本介绍,环境,选项,特殊字符
.shell变量 用户定义变量,环境变量,位置变量(shell 参数)
.shell script编程
条件测试,循环及重复控制
.shell定制
1.shell基础知识
作者:Stephen Bourne 在Bell实验室开发
建议:man sh 查看相关UNIX上的改进或特性
(1)shell提示符及其环境
/etc/passwd文件
提示符:$
/etc/profile $HOME/.profile
(2)shell执行选项
-n 测试shell script语法结构,只读取shell script但不执行
-x 进入跟踪方式,显示所执行的每一条命令,用于调度
-a Tag all variables for export
-c "string" 从strings中读取命令
-e 非交互方式
-f 关闭shell文件名产生功能
-h locate and remember functions as defind
-i 交互方式
-k 从环境变量中读取命令的参数
-r 限制方式
-s 从标准输入读取命令
-t 执行命令后退出(shell exits)
-u 在替换中如使用未定义变量为错误
-v verbose,显示shell输入行
这些选项可以联合使用,但有些显然相互冲突,如-e和-i.
(3)受限制shell(Restircted Shell)
sh -r 或 /bin/rsh
不能执行如下操作:cd, 更改PATH,指定全路径名,输出重定向,因此可以提供一个较
好的控制和安全机制。通常rsh用于应用型用户及拨号用户,这些用户通常是看不到提
示符的。通常受限制用户的主目录是不可写的。
不足:如果用户可以调用sh,则rsh的限制将不在起作用,事实上如果用户在vi及more
程序中调用shell,而这时rsh的限制将不再起作用。
(4)用set改变 shell选项
用户可以在$提示符下用set命令来设置或取消shell的选项。使用-设置选项,+取消相应
选项,大多数UNIX系统允许a,e,f,h,k,n,u,v和x的开关设置/取消。
set -xv
启动跟踪方式;显示所有的命令及替换,同样显示输入。
set -tu
关闭在替换时对未定义变量的检查。
使用echo $-显示所有已设置的shell选项。
(5)用户启动文件 .profile
PATH=$PATH:/usr/loacl/bin; export PATH
(6)shell环境变量
CDPATH 用于cd命令的查找路径
HOME /etc/passwd文件中列出的用户主目录
IFS Internal Field Separator,默认为空格,tab及换行符
MAIL /var/mail/$USERNAME mail等程序使用
PATH
PS1,PS2 默认提示符($)及换行提示符(>)
TERM 终端类型,常用的有vt100,ansi,vt200,xterm等
示例:$PS1="test:";export PS1
test: PS1="$";export PS1
$echo $MAIL
/var/mail/username
(7)保留字符及其含义
$ shell变量名的开始,如$var
| 管道,将标准输出转到下一个命令的标准输入
# 注释开始
& 在后台执行一个进程
? 匹配一个字符
* 匹配0到多个字符(与DOS不同,可在文件名中间使用,并且含.)
$- 使用set及执行时传递给shell的标志位
$! 最后一个子进程的进程号
$# 传递给shell script的参数个数
$* 传递给shell script的参数
$@ 所有参数,个别的用双引号括起来
$? 上一个命令的返回代码
当前shell的名字
$n (n:1-) 位置参数
$$ 进程标识号(Process Identifier Number, PID)
>file 输出重定向
`command` 命令替换,如 filename=`basename /usr/local/bin/tcsh`
>>fiile 输出重定向,append
转义符及单引号:
$echo "$HOME $PATH"
/home/hbwork /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:
$echo '$HOME $PATH'
$HOME $PATH
$echo $HOME $PATH
$HOME /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hbwork/bin
其他:
$dir=ls
$$dir
$alias dir ls
$dir
ls > filelist
ls >> filelist
wc -l < filelist
wc -l filelist
sleep 5; echo 5 seconds reaches; ls -l
ps ax |egrep inetd
find / -name core -exec rm {} \; &
filename=`date "+%Y%m%d"`.log
2. shell变量
变量:代表某些值的符号,如$HOME,cd命令查找$HOME,在计算机语言中可以使用变量可以
进行多种运算和控制。
Bourne Shell有如下四种变量:
.用户自定义变量
.位置变量即 shell script之参数
.预定义变量(特殊变量)
.环境变量(参考shell定制部分)
(1)用户自定义变量(数据的存储)
$ COUNT=1
$ NAME="He Binwu"
技巧:因为大部分UNIX命令使用小写字符,因此在shell编程中通常使用全大写变量,
当然这并不是强制性的,但使用大写字符可以在编程中方便地识别变量。
变量的调用:在变量前加$
$ echo $HOME
/home/hbwork
$ WEEK=Satur
$ echo Today is $WEEKday
Today is
$echo Today is ${WEEK}day
Today is Saturday
Shell变量赋值从右从左进行(Linux Shell/bash从左向右赋值!)
$ X=$Y Y=y
$ echo $X
y
$ Z=z Y=$Z
$ echo $Y
$
使用unset命令删除变量的赋值
$ Z=hello
$ echo $Z
hello
$ unset Z
$ echo $Z
$
有条件的命令替换
在Bourne Shell中可以使变量替换在特定条件下执行,即有条件的环境变量替换。
这种变量替换总是用大括号括起来的。
.设置变量的默认值
在变量未赋值之前其值为空。Bourne Shell允许对变量设置默认值,其格式如下:
${variable:-defaultvalue}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:-there}
Hello there
$ echo $UNAME #变量值并未发生变化
$ UNAME=hbwork
$ echo Hello ${UNAME:-there}
Hello hbwork
$
.另一种情况:改变变量的值,格式如下:
${variable:=value}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:=there}
Hello there
$ echo $UNAME #变量值并未发生变化
there
$
.变量替换中使用命令替换
$USERDIR=${$MYDIR:-`pwd`}
.在变量已赋值时进行替换 ${variable:+value}
.带有错误检查的有条件变量替换
${variable:?value}
例:
$ UNAME=
$ echo ${UNAME:?"UNAME has not been set"}
UNAME: UNAME has not been set
$ echo ${UNAME:?}
UNAME: parameter null or not set
(2)位置变量(Shell参数)
在shell script中位置参数可用..表示,表示内容通常为当前执行程序的文件名。
.防止变量值被替换 readonly variable
.使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shell
将为其设置一个新的环境让其执行,这称之了subshell. 在Bourne Shell中变量通常
被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。使
用export对subshell可用。
例:对变量PS1的export操作,shell的提示符将发生变化。
$ PS1=`hostname`$
peony$sh
$ echo $PS1
$ <-输出结果
$ exit
peony$export PS1
peony$sh
peony$ echo $PS1
peony$ <-输出结果
peony$
3.Shell Script编程
目的:使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。
(1)最简单的Shell 编程
$ls -R / |grep myname |more
每天数据的备份:
$ cd /usr/yourname; ls * |cpio -o > /dev/rmt/0h
书写程序的目的是一次编程,多次使用(执行)!
$ cat > backup.sh
cd /home/hbwork
ls * | cpio -o > /dev/rmt/0h
^D
执行:
$ sh backup.sh
或:
$ chmod +x backup.sh
$ ./backup.sh
技巧:在shell script中加入必要的注释,以便以后阅读及维护。
(2)shell是一个(编程)语言
同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shell script
编程更为有用,如:数据变量、参数传递、判断、流程控制、数据输入和输出,子
程序及以中断处理等。
. 在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中:
cd $WORKDIR
ls * | cpio -o > /dev/rmt/0h
. Shell编程中的注释以#开头
. 对shell变量进行数字运算,使用expr命令
expr integer operator integer
其中operator为+ - * / %, 但对*的使用要用转义符\,如:
$expr 4 \* 5
20
$int=`expr 5 + 7`
$echo $int
12
(3)Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read)
restoreall.sh 对backup.sh程序的备份磁带进行恢复
$cat > restoreall.sh
cd $WORKDIR
cpio -i < /dev/rmt/0h
^D
restore1.sh:只能恢复个文件
#restore1 --program to restore a single file
cd $WORKDIR
cpio -i $i < /dev/rmt/0h
$restore1 file1
恢复多个文件restoreany :
#restoreany --program to restore a single file
cd $WORKDIR
cpio -i $* < /dev/rmt/0h
$ restoreany file1 file2 file3
(4)条件判断
. if-then语句,格式如下:
if command_1
then
command_2
command_3
fi
command_4
在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和
command_3,而command_4总是执行.
示例程序unload: 在备份成功时删除原始文件,带有错误检查
cd
#备份时未考虑不成功的情况!
ls -a | cpio -o > /dev/rmt/0h
rm -rf *
改进如下:
#当使用了管道命令时,管理命令的返回代码为最后一个命令的返回代码
if ls -a | cpio -o > /dev/rmt/0h
then
rm -rf *
fi
. if-then-else语句
if command_1
then
command_2
else
command_3
fi
技巧: 由于shell对命令中的多余的空格不作任何处理,一个好的程序员会用这一特性
对自己的程序采用统一的缩进格式,以增强自己程序的可读性.
. 使用test命令进行进行条件测试
格式: test conditions
test在以下四种情况下使用: a. 字符比较 b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常民政部下均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号将整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
例: 重写unload程序,使用test测试
#!/bin/sh
#unload - program to backup and remove files
#syntax: unload directory
#check arguments
if [ $# -ne 1 ]
then
echo "usage: directory"
exit 1
fi
#check for valid directory name
if [ ! -d "" ]
then
echo " is not a directory"
exit 2
fi
cd
ls -a | cpio -o > /dev/rmt/0h
if [ $? -eq 0 ]
then
rm -rf *
else
echo "A problem has occured in creating backup"
echo "The directory will not be ereased"
echo "Please check the backup device"
exit 3
fi
# end of unload
在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二是
设置程序的退出状态
g. if嵌套及elif结构
if command
then
command
else
if command
then
command
else
if command
then
command
fi
fi
fi
改进:使用elif结构
if command
then
command
elif command
then
command
elif command
then
command
fi
elif结构同if结构类似,但结构更清淅,其执行结果完全相同.
-------------未完,待续----------
表示内容通常为当前执行程序的文件名。
.防止变量值被替换 readonly variable
.使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shell
将为其设置一个新的环境让其执行,这称之了subshell. 在Bourne Shell中变量通常
被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。使
用export对subshell可用。
例:对变量PS1的export操作,shell的提示符将发生变化。
$ PS1=`hostname`$
peony$sh
$ echo $PS1
$ <-输出结果
$ exit
peony$export PS1
peony$sh
peony$ echo $PS1
peony$ <-输出结果
peony$
3.Shell Script编程
目的:使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。
(1)最简单的Shell 编程
$ls -R / |grep myname |more
每天数据的备份:
$ cd /usr/yourname; ls * |cpio -o > /dev/rmt/0h
书写程序的目的是一次编程,多次使用(执行)!
$ cat > backup.sh
cd /home/hbwork
ls * | cpio -o > /dev/rmt/0h
^D
执行:
$ sh backup.sh
或:
$ chmod +x backup.sh
$ ./backup.sh
技巧:在shell script中加入必要的注释,以便以后阅读及维护。
(2)shell是一个(编程)语言
同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shell script
编程更为有用,如:数据变量、参数传递、判断、流程控制、数据输入和输出,子
程序及以中断处理等。
. 在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中:
cd $WORKDIR
ls * | cpio -o > /dev/rmt/0h
. Shell编程中的注释以#开头
. 对shell变量进行数字运算,使用expr命令
expr integer operator integer
其中operator为+ - * / %, 但对*的使用要用转义符\,如:
$expr 4 \* 5
20
$int=`expr 5 + 7`
$echo $int
12
(3)Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read)
restoreall.sh 对backup.sh程序的备份磁带进行恢复
$cat > restoreall.sh
cd $WORKDIR
cpio -i < /dev/rmt/0h
^D
restore1.sh:只能恢复一个文件
#restore1 --program to restore a single file
cd $WORKDIR
cpio -i $i < /dev/rmt/0h
$restore1 file1
恢复多个文件restoreany :
#restoreany --program to restore a single file
cd $WORKDIR
cpio -i $* < /dev/rmt/0h
$ restoreany file1 file2 file3
(4)条件判断
. if-then语句,格式如下:
if command_1
then
command_2
command_3
fi
command_4
在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和
command_3,而command_4总是执行.
示例程序unload: 在备份成功时删除原始文件,带有错误检查
cd
#备份时未考虑不成功的情况!
ls -a | cpio -o > /dev/rmt/0h
rm -rf *
改进如下:
#当使用了管道命令时,管理命令的返回代码为最后一命令的返回代码
if ls -a | cpio -o > /dev/rmt/0h
then
rm -rf *
fi
. if-then-else语句
if command_1
then
command_2
else
command_3
fi
技巧: 由于shell对命令中的多余的空格不作任何处理,一个好的程序员会用这一特性
对自己的程序采用统一的缩进格式,以增强自己程序的可读性.
. 使用test命令进行进行条件测试
格式: test conditions
test在以下四种情况下使用: a. 字符比较 b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常民政部下均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号将整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
例: 重写unload程序,使用test测试
#!/bin/sh
#unload - program to backup and remove files
#syntax: unload directory
#check arguments
if [ $# -ne 1 ]
then
echo "usage:
Bourne Shell
介绍:Bourne Shell 基础及其他很多有用的特性,shell编程及组织。
主要内容:
.shell基础 基本介绍,环境,选项,特殊字符
.shell变量 用户定义变量,环境变量,位置变量(shell 参数)
.shell script编程
条件测试,循环及重复控制
.shell定制
1.shell基础知识
作者:Stephen Bourne 在Bell实验室开发
建议:man sh 查看相关UNIX上的改进或特性
(1)shell提示符及其环境
/etc/passwd文件
提示符:$
/etc/profile $HOME/.profile
(2)shell执行选项
-n 测试shell script语法结构,只读取shell script但不执行
-x 进入跟踪方式,显示所执行的每一条命令,用于调度
-a Tag all variables for export
-c "string" 从strings中读取命令
-e 非交互方式
-f 关闭shell文件名产生功能
-h locate and remember functions as defind
-i 交互方式
-k 从环境变量中读取命令的参数
-r 限制方式
-s 从标准输入读取命令
-t 执行命令后退出(shell exits)
-u 在替换中如使用未定义变量为错误
-v verbose,显示shell输入行
这些选项可以联合使用,但有些显然相互冲突,如-e和-i.
(3)受限制shell(Restircted Shell)
sh -r 或 /bin/rsh
不能执行如下操作:cd, 更改PATH,指定全路径名,输出重定向,因此可以提供一个较
好的控制和安全机制。通常rsh用于应用型用户及拨号用户,这些用户通常是看不到提
示符的。通常受限制用户的主目录是不可写的。
不足:如果用户可以调用sh,则rsh的限制将不在起作用,事实上如果用户在vi及more
程序中调用shell,而这时rsh的限制将不再起作用。
(4)用set改变 shell选项
用户可以在$提示符下用set命令来设置或取消shell的选项。使用-设置选项,+取消相应
选项,大多数UNIX系统允许a,e,f,h,k,n,u,v和x的开关设置/取消。
set -xv
启动跟踪方式;显示所有的命令及替换,同样显示输入。
set -tu
关闭在替换时对未定义变量的检查。
使用echo $-显示所有已设置的shell选项。
(5)用户启动文件 .profile
PATH=$PATH:/usr/loacl/bin; export PATH
(6)shell环境变量
CDPATH 用于cd命令的查找路径
HOME /etc/passwd文件中列出的用户主目录
IFS Internal Field Separator,默认为空格,tab及换行符
MAIL /var/mail/$USERNAME mail等程序使用
PATH
PS1,PS2 默认提示符($)及换行提示符(>)
TERM 终端类型,常用的有vt100,ansi,vt200,xterm等
示例:$PS1="test:";export PS1
test: PS1="$";export PS1
$echo $MAIL
/var/mail/username
(7)保留字符及其含义
$ shell变量名的开始,如$var
| 管道,将标准输出转到下一个命令的标准输入
# 注释开始
& 在后台执行一个进程
? 匹配一个字符
* 匹配0到多个字符(与DOS不同,可在文件名中间使用,并且含.)
$- 使用set及执行时传递给shell的标志位
$! 最后一个子进程的进程号
$# 传递给shell script的参数个数
$* 传递给shell script的参数
$@ 所有参数,个别的用双引号括起来
$? 上一个命令的返回代码
当前shell的名字
$n (n:1-) 位置参数
$$ 进程标识号(Process Identifier Number, PID)
>file 输出重定向
`command` 命令替换,如 filename=`basename /usr/local/bin/tcsh`
>>fiile 输出重定向,append
转义符及单引号:
$echo "$HOME $PATH"
/home/hbwork /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:
$echo '$HOME $PATH'
$HOME $PATH
$echo $HOME $PATH
$HOME /opt/kde/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/hbwork/bin
其他:
$dir=ls
$$dir
$alias dir ls
$dir
ls > filelist
ls >> filelist
wc -l < filelist
wc -l filelist
sleep 5; echo 5 seconds reaches; ls -l
ps ax |egrep inetd
find / -name core -exec rm {} \; &
filename=`date "+%Y%m%d"`.log
2. shell变量
变量:代表某些值的符号,如$HOME,cd命令查找$HOME,在计算机语言中可以使用变量可以
进行多种运算和控制。
Bourne Shell有如下四种变量:
.用户自定义变量
.位置变量即 shell script之参数
.预定义变量(特殊变量)
.环境变量(参考shell定制部分)
(1)用户自定义变量(数据的存储)
$ COUNT=1
$ NAME="He Binwu"
技巧:因为大部分UNIX命令使用小写字符,因此在shell编程中通常使用全大写变量,
当然这并不是强制性的,但使用大写字符可以在编程中方便地识别变量。
变量的调用:在变量前加$
$ echo $HOME
/home/hbwork
$ WEEK=Satur
$ echo Today is $WEEKday
Today is
$echo Today is ${WEEK}day
Today is Saturday
Shell变量赋值从右从左进行(Linux Shell/bash从左向右赋值!)
$ X=$Y Y=y
$ echo $X
y
$ Z=z Y=$Z
$ echo $Y
$
使用unset命令删除变量的赋值
$ Z=hello
$ echo $Z
hello
$ unset Z
$ echo $Z
$
有条件的命令替换
在Bourne Shell中可以使变量替换在特定条件下执行,即有条的环境变量替换。
这种变量替换总是用大括号括起来的。
.设置变量的默认值
在变量未赋值之前其值为空。Bourne Shell允许对变量设置默认值,其格式如下:
${variable:-defaultvalue}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:-there}
Hello there
$ echo $UNAME #变量值并未发生变化
$ UNAME=hbwork
$ echo Hello ${UNAME:-there}
Hello hbwork
$
.另一种情况:改变变量的值,格式如下:
${variable:=value}
例:
$ echo Hello $UNAME
Hello
$ echo Hello ${UNAME:=there}
Hello there
$ echo $UNAME #变量值并未发生变化
there
$
.变量替换中使用命令替换
$USERDIR=${$MYDIR:-`pwd`}
.在变量已赋值时进行替换 ${variable:+value}
.带有错误检查的有条件变量替换
${variable:?value}
例:
$ UNAME=
$ echo ${UNAME:?"UNAME has not been set"}
UNAME: UNAME has not been set
$ echo ${UNAME:?}
UNAME: parameter null or not set
(2)位置变量(Shell参数)
在shell script中位置参数可用..表示,表示内容通常为当前执行程序的文件名。
.防止变量值被替换 readonly variable
.使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shell
将为其设置一个新的环境让其执行,这称之了subshell. 在Bourne Shell中变量通常
被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。使
用export对subshell可用。
例:对变量PS1的export操作,shell的提示符将发生变化。
$ PS1=`hostname`$
peony$sh
$ echo $PS1
$ <-输出结果
$ exit
peony$export PS1
peony$sh
peony$ echo $PS1
peony$ <-输出结果
peony$
3.Shell Script编程
目的:使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。
(1)最简单的Shell 编程
$ls -R / |grep myname |more
每天数据的备份:
$ cd /usr/yourname; ls * |cpio -o > /dev/rmt/0h
书写程序的目的是一次编程,多次使用(执行)!
$ cat > backup.sh
cd /home/hbwork
ls * | cpio -o > /dev/rmt/0h
^D
执行:
$ sh backup.sh
或:
$ chmod +x backup.sh
$ ./backup.sh
技巧:在shell script中加入必要的注释,以便以后阅读及维护。
(2)shell是一个(编程)语言
同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shell script
编程更为有用,如:数据变量、参数传递、判断、流程控制、数据输入和输出,子
程序及以中断处理等。
. 在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中:
cd $WORKDIR
ls * | cpio -o > /dev/rmt/0h
. Shell编程中的注释以#开头
. 对shell变量进行数字运算,使用expr命令
expr integer operator integer
其中operator为+ - * / %, 但对*的使用要用转义符\,如:
$expr 4 \* 5
20
$int=`expr 5 + 7`
$echo $int
12
(3)Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read)
restoreall.sh 对backup.sh程序的备份磁带进行恢复
$cat > restoreall.sh
cd $WORKDIR
cpio -i < /dev/rmt/0h
^D
restore1.sh:只能恢复一个文件
#restore1 --program to restore a single file
cd $WORKDIR
cpio -i $i < /dev/rmt/0h
$restore1 file1
恢复多个文件restoreany :
#restoreany --program to restore a single file
cd $WORKDIR
cpio -i $* < /dev/rmt/0h
$ restoreany file1 file2 file3
(4)条件判断
. if-then语句,格式如下:
if command_1
then
command_2
command_3
fi
command_4
在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和
command_3,而command_4总是执行.
示例程序unload: 在备份成功时删除原始文件,带有错误检查
cd
#备份时未考虑不成功的情况!
ls -a | cpio -o > /dev/rmt/0h
rm -rf *
改进如下:
#当使用了管道命令时,管理命令的返回代码为最后一个命令的返回代码
if ls -a | cpio -o > /dev/rmt/0h
then
rm -rf *
fi
. if-then-else语句
if command_1
then
command_2
else
command_3
fi
技巧: 由于shell对命令中的多余的空格不作任何处理,一个好的程序员会用这一特性
对自己的程序采用统一的缩进格式,以增强自己程序的可读性.
. 使用test命令进行进行条件测试
格式: test conditions
test在以下四种情况下使用: a. 字符比较 b.两个整数值的比较
c. 文件操作,如文件是否存在及文件的状态等
d. 逻辑操作,可以进行and/or,与其他条件联合使用
a. 测试字符数据: shell变量通常民政部下均作为字符变量
str1 = str2 二者相长,相同
str1 != str2 不同
-n string string不为空(长度不为零)
-z string string为空
string string不为空
例:
$ str1=abcd #在含有空格时必须用引号括起来
$ test $str1=abcd
$ echo $?
0
$ str1="abcd "
$ test $str1=abcd
$ echo $?
1
Note: 在test处理含有空格的变量时最好用引号将变量括起来,否则会出现错误的结果,
因为shell在处理命令行时将会去掉多余的空格,而用引号括起来则可以防止
shell去掉这些空格.
例:
$ str1=" "
$ test $str1
$ echo $?
1
$ test "$str1"
$ echo $?
0
$ test -n $str1
test: argument expected
$ test -n "$str1"
$ echo $?
0
$
b. 整数测试: test与expr相同,可以将字符型变量转换为整数进行操作,expr进行
整数的算术运算,而test则进行逻辑运算.
表达式 说明
---------------------------------------
int1 -eq int2 相等?
int1 -ne int2 不等?
int1 -gt int2 int1 > int2 ?
int1 -ge int2 int1 >= int2 ?
int1 -lt int2 int1 < int2 ?
int1 -le int2 int1 <= int2 ?
例:
$ int1=1234
$ int2=01234
$ test $int1 -eq $int2
$ echo $?
0
c. 文件测试:检查文件状态如存在及读写权限等
-r filename 用户对文件filename有读权限?
-w filename 用户对文件filename有写权限?
-x filename 用户对文件filename有可执行权限?
-f filename 文件filename为普通文件?
-d filename 文件filename为目录?
-c filename 文件filename为字符设备文件?
-b filename 文件filename为块设备文件?
-s filename 文件filename大小不为零?
-t fnumb 与文件描述符fnumb(默认值为1)相关的设备是一个终端设备?
d. 测试条件之否定,使用!
例:
$ cat /dev/null > empty
$ test -r empty
$ echo $?
0
$ test -s empty
1
$ test ! -s empty
$ echo $?
0
e. 测试条件之逻辑运算
-a And
-o Or
例: $ test -r empty -a -s empty
$ echo $?
1
f. 进行test测试的标准方法
因为test命令在 shell编程中占有很重要的地位,为了使shell能同其他编程语言一样
便于阅读和组织, Bourne Shell在使用test测试时使用了另一种方法:用方括号将整个
test测试括起来:
$ int1=4
$ [ $int1 -gt 2 ]
$ echo $?
0
例: 重写unload程序,使用test测试
#!/bin/sh
#unload - program to backup and remove files
#syntax: unload directory
#check arguments
if [ $# -ne 1 ]
then
echo "usage: directory"
exit 1
fi
#check for valid directory name
if [ ! -d "" ]
then
echo " is not a directory"
exit 2
fi
cd
ls -a | cpio -o > /dev/rmt/0h
if [ $? -eq 0 ]
then
rm -rf *
else
echo "A problem has occured in creating backup"
echo "The directory will not be ereased"
echo "Please check the backup device"
exit 3
fi
# end of unload
在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二是
设置程序的退出状态
g. if嵌套及elif结构
if command
then
command
else
if command
then
command
else
if command
then
command
fi
fi
fi
改进:使用elif结构
if command
command
elif command
then
command
elif command
then
command
fi
elif结构同if结构类似,但结构更清淅,其执行结果完全相同.
-------------未完,待续----------
directory"
exit 1
fi
#check for valid directory name
if [ ! -d "" ]
then
echo " is not a directory"
exit 2
fi
cd
ls -a | cpio -o > /dev/rmt/0h
if [ $? -eq 0 ]
then
rm -rf *
else
echo "A problem has occured in creating backup"
echo "The directory will not be ereased"
echo "Please check the backup device"
exit 3
fi
# end of unload
在如上示例中出现了exit, exit有两个作用:一是停止程序中其他命令的执行,二是
设置程序的退出状态
g. if嵌套及elif结构
if command
then
command
else
if command
then
command
else
if command
then
command
fi
fi
fi
改进:使用elif结构
if command
then
command
elif command
then
command
elif command
then
command
fi
elif结构同if结构类似,但结构更清淅,其执行结果完全相同.
-------------未完,待续----------
|
|
|||
How to write a shell script
find . -name file -printand you'd rather type a simple command, say
sfind fileCreate a shell script
% cd ~/bin
% emacs sfind
% page sfind
find . -name $1 -print
% chmod a+x sfind
% rehash
% cd /usr/local/bin
% sfind tcsh
./shells/tcsh
%chmod a+x sfind
#!/bin/shFrom the man page for exec(2):
"On the first line of an interpreter script, following the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script."
You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but these days there are many interpretors -- Csh, Ksh, Bash, and others.
PATH=/usr/ucb:/usr/bin:/bin; export PATHA PATH specification is recommended -- often times a script will fail for some people because they have a different or incomplete search path.
The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command.
if [ $# -ne 3 ]; thenThis script requires three arguments and gripes accordingly.
echo 1>&2 Usage: $0 19 Oct 91
exit 127
fi
# is the year out of range for me?A non-zero exit status indicates an error condition of some sort while a zero exit status indicates things worked as expected.
if [ $year -lt 1901 -o $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of range
exit 127
fi
etc...
# All done, exit ok
exit 0
On BSD systems there's been an attempt to categorize some of the more common exit status codes. See /usr/include/sysexits.h.
The conditional construct is:
if command; thenFor example,
command
fi
if tty -s; thenYour code should be written with the expectation that others will use it. Making sure you return a meaningful exit status will help.
echo Enter text end with \^D
fi
# is the year out of range for me?Error messages should appear on stderr not on stdout! Output should appear on stdout. As for input/output dialogue:
if [ $year -lt 1901 -o $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of my range
exit127
fi
etc...
# ok, you have the number of days since Jan 1, ...
case `expr $days % 7` in
0)
echo Mon;;
1)
echo Tue;;
etc...
# give the fellow a chance to quitNote: this code behaves differently if there's a user to communicate with (ie. if the standard input is a tty rather than a pipe, or file, or etc. See tty(1)).
if tty -s ; then
echo This will remove all files in $* since ...
echo $n Ok to procede? $c; read ans
case "$ans" in
n*|N*)
echo File purge abandoned;
exit 0 ;;
esac
RM="rm -rfi"
else
RM="rm -rf"
fi
Substitute values for variable and perform task:
for variable in word ...For example:
do
command
done
for i in `cat $LOGS`Alternatively you may see:
do
mv $i $i.$TODAY
cp /dev/null $i
chmod 664 $i
done
for variable in word ...; do command; done
Switch to statements depending on pattern match
case word inFor example:
[ pattern [ | pattern ... ] )
command ;; ] ...
esac
case "$year" in
[0-9][0-9])
year=19${year}
years=`expr $year - 1901`
;;
[0-9][0-9][0-9][0-9])
years=`expr $year - 1901`
;;
*)
echo 1>&2 Year \"$year\" out of range ...
exit 127
;;
esac
Test exit status of command and branch
if commandFor example:
then
command
[ else
command ]
fi
if [ $# -ne 3 ]; thenAlternatively you may see:
echo 1>&2 Usage: $0 19 Oct 91
exit 127
fi
if command; then command; [ else command; ] fi
Repeat task while command returns good exit status.
{while | until} command
do
command
done
For example:
# for each argument mentioned, purge that directoryAlternatively you may see:
while [ $# -ge 1 ]; do
_purge $1
shift
done
while command; do command; done
Variables are sequences of letters, digits, or underscores beginning with a letter or underscore. To get the contents of a variable you must prepend the name with a $.
Numeric variables (eg. like $1, etc.) are positional vari- ables for argument communication.
Assign a value to a variable by variable=value. For example:
PATH=/usr/ucb:/usr/bin:/bin; export PATHor
TODAY=`(set \`date\`; echo $1)`
Variables are not exported to children unless explicitly marked.
# We MUST have a DISPLAY environment variableLikewise, for variables like the PRINTER which you want hon- ored by lpr(1). From a user's .profile:
if [ "$DISPLAY" = "" ]; then
if tty -s ; then
echo "DISPLAY (`hostname`:0.0)? \c";
read DISPLAY
fi
if [ "$DISPLAY" = "" ]; then
DISPLAY=`hostname`:0.0
fi
export DISPLAY
fi
PRINTER=PostScript; export PRINTERNote: that the Cshell exports all environment variables.
Use $variable (or, if necessary, ${variable}) to reference the value.
# Most user's have a /bin of their ownThe braces are required for concatenation constructs.
if [ "$USER" != "root" ]; then
PATH=$HOME/bin:$PATH
else
PATH=/etc:/usr/etc:$PATH
fi
$p_01The value of the variable "p_01".
${p}_01
The value of the variable "p" with "_01" pasted onto the end.
${variable-word}
If the variable has been set, use it's value, else use word.
POSTSCRIPT=${POSTSCRIPT-PostScript};
export POSTSCRIPT
${variable:-word}
If the variable has been set and is not null, use it's value, else use word.
These are useful constructions for honoring the user envi- ronment. Ie. the user of the script can override variable assignments. Cf. programs like lpr(1) honor the PRINTER environment variable, you can do the same trick with your shell scripts.
${variable:?word}
If variable is set use it's value, else print out word and exit. Useful for bailing out.
Command line arguments to shell scripts are positional vari- ables:
$0, $1, ...The command and arguments. With $0 the command and the rest the arguments.
$#The number of arguments.
$*, $@All the arguments as a blank separated string. Watch out for "$*" vs. "$@".
shiftShift the postional variables down one and decrement number of arguments.
set arg arg ...Set the positional variables to the argument list.
Command line parsing uses shift:
# parse argument listA use of the set command:
while [ $# -ge 1 ]; do
case $1 in
process arguments...
esac
shift
done
# figure out what day it is
TODAY=`(set \`date\`; echo $1)`
cd $SPOOL
for i in `cat $LOGS`
do
mv $i $i.$TODAY
cp /dev/null $i
chmod 664 $i
done
$$Current process id. This is very useful for constructing temporary files.
tmp=/tmp/cal0$$The exit status of the last command.
trap "rm -f $tmp /tmp/cal1$$ /tmp/cal2$$"
trap exit 1 2 13 15
/usr/lib/calprog >$tmp
$?
$command
# Run target file if no errors and ...
if [ $? -eq 0 ]
then
etc...
fi
Special characters to terminate words:
; & ( ) | ^ < > new-line space tabThese are for command sequences, background jobs, etc. To quote any of these use a backslash (\) or bracket with quote marks ("" or '').
Single Quotes
Within single quotes all characters are quoted -- including the backslash. The result is one word.
grep :${gid}: /etc/group | awk -F: '{print $1}'
Double QuotesWithin double quotes you have variable subsitution (ie. the dollar sign is interpreted) but no file name generation (ie. * and ? are quoted). The result is one word.
if [ ! "${parent}" ]; then
prent=${people}/${group}/${user}
fi
Back QuotesBack quotes mean run the command and substitute the output.
if [ "`echo -n`" = "-n" ]; thenand
n=""
c="\c"
else
n="-n"
c=""
fi
TODAY=`(set \`date\`; echo $1)`
Functions are a powerful feature that aren't used often enough. Syntax is
name ()For example:
{
commands
}
# Purge a directoryWithin a function the positional parmeters $0, $1, etc. are the arguments to the function (not the arguments to the script).
_purge()
{
# there had better be a directory
if [ ! -d $1 ]; then
echo $1: No such directory 1>&2
return
fi
etc...
}
Within a function use return instead of exit.
Functions are good for encapsulations. You can pipe, redi- rect input, etc. to functions. For example:
# deal with a file, add people one at a time
do_file()
{
while parse_one
etc...
}
etc...
# take standard input (or a specified file) and do it.
if [ "$1" != "" ]; then
cat $1 | do_file
else
do_file
fi
You can execute shell scripts from within shell scripts. A couple of choices:
sh command
This runs the shell script as a separate shell. For example, on Sun machines in /etc/rc:
sh /etc/rc.local. command
This runs the shell script from within the current shell script. For example:
# Read in configuration informationWhat are the virtues of each? What's the difference? The second form is useful for configuration files where environment variable are set for the script. For example:
. /etc/hostconfig
for HOST in $HOSTS; doUsing configuration files in this manner makes it possible to write scripts that are automatically tailored for differ- ent situations.
# is there a config file for this host?
if [ -r ${BACKUPHOME}/${HOST} ]; then
. ${BACKUPHOME}/${HOST}
fi
etc...
The most powerful command is test(1).
if test expression; thenand (note the matching bracket argument)
etc...
if [ expression ]; thenOn System V machines this is a builtin (check out the com- mand /bin/test).
etc...
On BSD systems (like the Suns) compare the command /usr/bin/test with /usr/bin/[.
Useful expressions are:
test { -w, -r, -x, -s, ... } filename
is file writeable, readable, executeable, empty, etc?
test n1 { -eq, -ne, -gt, ... } n2
are numbers equal, not equal, greater than, etc.?
test s1 { =, != } s2
Are strings the same or different?
test cond1 { -o, -a } cond2
Binary or; binary and; use ! for unary negation.
For example
if [ $year -lt 1901 -o $year -gt 2099 ]; thenLearn this command inside out! It does a lot for you.
echo 1>&2 Year \"$year\" out of range
exit 127
fi
The test command provides limited string matching tests. A more powerful trick is to match strings with the case switch.
# parse argument listOf course getopt would work much better.
while [ $# -ge 1 ]; do
case $1 in
-c*) rate=`echo $1 | cut -c3-`;;
-c) shift; rate=$1 ;;
-p*) prefix=`echo $1 | cut -c3-`;;
-p) shift; prefix=$1 ;;
-*) echo $Usage; exit 1 ;;
*) disks=$*; break ;;
esac
shift
done
On BSD systems to get a prompt you'd say:
echo -n Ok to procede?; read ansOn SysV systems you'd say:
echo Ok to procede? \c; read ansIn an effort to produce portable code we've been using:
# figure out what kind of echo to use
if [ "`echo -n`" = "-n" ]; then
n=""; c="\c"
else
n="-n"; c=""
fi
etc...
echo $n Ok to procede? $c; read ans
The Unix tradition is that programs should execute as qui- etly as possible. Especially for pipelines, cron jobs, etc.
User prompts aren't required if there's no user.
# If there's a person out there, prod him a bit.The tradition also extends to output.
if tty -s; then
echo Enter text end with \^D
fi
# If the output is to a terminal, be verboseBeware: just because stdin is a tty that doesn't mean that stdout is too. User prompts should be directed to the user terminal.
if tty -s <&1; then
verbose=true
else
verbose=false
fi
# If there's a person out there, prod him a bit.Have you ever had a program stop waiting for keyboard input when the output is directed elsewhere?
if tty -s; then
echo Enter text end with \^D >&0
fi
We're familiar with redirecting input. For example:
# take standard input (or a specified file) and do it.alternatively, redirection from a file:
if [ "$1" != "" ]; then
cat $1 | do_file
else
do_file
fi
# take standard input (or a specified file) and do it.You can also construct files on the fly.
if [ "$1" != "" ]; then
do_file < $1
else
do_file
fi
rmail bsmtp <Note: that variables are expanded in the input.<$1@newshost.uwo.ca>
rcpt to:
data
from: <$1@newshost.uwo.ca>
to:
Subject: Signon $2
subscribe $2 Usenet Feeder at UWO
.
quit
EOF
One of the more common things you'll need to do is parse strings. Some tricks
TIME=`date | cut -c12-19`With some care, redefining the input field separators can help.
TIME=`date | sed 's/.* .* .* \(.*\) .* .*/\1/'`
TIME=`date | awk '{print $4}'`
TIME=`set \`date\`; echo $4`
TIME=`date | (read u v w x y z; echo $x)`
#!/bin/sh
# convert IP number to in-addr.arpa name
name()
{ set `IFS=".";echo $1`
echo $4.$3.$2.$1.in-addr.arpa
}
if [ $# -ne 1 ]; then
echo 1>&2 Usage: bynum IP-address
exit 127
fi
add=`name $1`
nslookup < < EOF | grep "$add" | sed 's/.*= //'
set type=any
$add
EOF
The shell has a number of flags that make debugging easier:
sh -n command
Read the shell script but don't execute the commands. IE. check syntax.
sh -x command
Display commands and arguments as they're executed. In a lot of my shell scripts you'll see
# Uncomment the next line for testing
# set -x
Based on An Introduction to Shell Programing by:
Reg Quinton
用rsync对网站进行镜像备份[转]
用C语言进行CGI程序设计
Linux 守护进程的编程方法
一些c 常见技巧
c语言库函数syslog--将信息记录至系统日志文件
调用 动态链接库 示例
关于C++ 和 C 接口互相调用的问题 extern
关于接口互相调用的问题 首先讨论C++ 调用 C 的库
1。 C库 的头文件 .h
#ifndef LIBWORDSFQ
#define LIBWORDSFQ
typedef struct{
char *words;
int wordsfq;
}wordsfqinfo;
#ifdef __cplusplus
extern "C"{
#endif
long Searchfq( int inputword_number, void * wordsfqlist );
void Init( const char *conffilename );
void Release();
#ifdef __cplusplus
}
#endif
#endif
2。c 调用 C++库
.h 文件
#ifndef _LIKELIHOOD_H
#define _LIKELIHOOD_H
extern "C" {
long learn(const char *libpath, const char *filename,const char *searchfile,const char *stopword);
long evaluate(const char *libpath, const char *filename, const char *stopword);
}
#endif
具体情况参见