2010年12月4日土曜日

linux shell 笔记二

流程控制
if表达式
if...;then
  ...
elif...;then
  ...
else
  ...
fi
大多数情况下,可以使用测试命令来对条件进行测试。比如可以比较字符串、判断文件是否存在及是否可读等等…
通常用" [ ] "来表示条件测试。注意这里的空格很重要。要确保方括号的空格。
[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a和$b是否相等
条件判断:expression为字符串操作
-n str   字符串str是否不为空
-z str   字符串str是否为空
str1 =str2  str1是否与str2相同
str1!=str2  str1是否与str2不同
条件判断:expression为整数操作
expr1 -a expr2  如果 expr1 和 expr2 评估为真,则为真
expr1 -o expr2   如果 expr1 或 expr2 评估为真,则为真
条件判断:expression为bool操作
int1 -eq int2  如果int1等于int2,则为真
int1 -ge int2  如果int1大于或等于int2,则为真
int1 -gt int2  如果int1大于int2 ,则为真
int1 -le int2  如果int1小于或等于int2 ,则为真
int1 -lt int2  如果int1小于int2 ,则为真
int1 -ne int2  如果int1不等于int2 ,则为真
条件判断:expression为文件操作
-b  是否块文件     -p  文件是否为一个命名管道
-c  是否字符文件   -r  文件是否可读
-d  是否一个目录   -s  文件的长度是否不为零
-e  文件是否存在   -S  是否为套接字文件
-f  是否普通文件   -x  文件是否可执行,则为真
-g  是否设置了文件的 SGID 位     -u  是否设置了文件的 SUID 位
-G  文件是否存在且归该组所有     -w  文件是否可写,则为真
-k  文件是否设置了的粘贴位       -O  文件是否存在且归该用户所有
-t fd fd 是否是一个与终端相连的打开的文件描述符(fd 默认为 1) 



-b file       True if file exists and is a block special file.
-c file       True if file exists and is a character special file.
-d file       True if file exists and is a directory.
-e file       True if file exists (regardless of type).
-f file       True if file exists and is a regular file.
-g file       True if file exists and its set group ID flag is set.
-h file       True if file exists and is a symbolic link.  This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use -L instead.
-k file       True if file exists and its sticky bit is set.
-p file       True if file is a named pipe (FIFO).
-r file       True if file exists and is readable.
-s file       True if file exists and has a size greater than zero.
-t file_descriptor  True if the file whose file descriptor number is file_descriptor is open and is associated with a terminal.
-u file       True if file exists and its set user ID flag is set.
-w file       True if file exists and is writable.  True indicates only that the write flag is on.  The file is not writable on a read-only file system even if this test indicates true.
-x file       True if file exists and is executable.  True indicates only that the execute flag is on.  If file is a directory, true indicates that file can be searched.
-L file       True if file exists and is a symbolic link.
-O file       True if file exists and its owner matches the effective user id of this process.
-G file       True if file exists and its group matches the effective group id of this process.
-S file       True if file exists and is a socket.


-z string     True if the length of string is zero.
-n string     True if the length of string is nonzero.


file1 -nt file2 True if file1 exists and is newer than file2.
file1 -ot file2 True if file1 exists and is older than file2.
file1 -ef file2 True if file1 and file2 exist and refer to the same file.


string        True if string is not the null string.
s1 = s2       True if the strings s1 and s2 are identical.
s1 != s2      True if the strings s1 and s2 are not identical.
s1 < s2       True if string s1 comes before s2 based on the ASCII value of their characters.
s1 > s2       True if string s1 comes after s2 based on the ASCII value of their characters.


n1 -eq n2     True if the integers n1 and n2 are algebraically equal.
n1 -ne n2     True if the integers n1 and n2 are not algebraically equal.
n1 -gt n2     True if the integer n1 is algebraically greater than the integer n2.
n1 -ge n2     True if the integer n1 is algebraically greater than or equal to the integer n2.
n1 -lt n2     True if the integer n1 is algebraically less than the integer n2.
n1 -le n2     True if the integer n1 is algebraically less than or equal to the integer n2.

! expression  True if expression is false.
expression1 -a expression2 True if both expression1 and expression2 are true.
expression1 -o expression2 True if either expression1 or expression2 are true.
(expression)  True if expression is true.

0 件のコメント:

コメントを投稿