当前位置: > shell编程 >

shell条件判断(shell if语句与case语句)的例子

时间:2015-03-14 21:56来源:linux.it.net.cn 作者:IT
有关shell if条件判断语句的用法,shell条件判断中case语句的实例代码,shell if常用判断条件,shell常用类型的判断写法。


1、if条件判断(shell if语句)

if条件判断: if [ 条件 ]; then do something fi 多个条件:
&& 代表AND || 代表OR
多重判断: if [ 条件 ];then do something elif [ 条件2 ]; 
then //do something else //do someshing fi

例子:
 

 

#!/bin/bash

read -p "Please input (Y/N) : " yn

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "Yes!"
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "No!"
else
echo "Input Error"
fi

exit 0

结果:
 

[work@www sh]$ sh hello.sh
Please input (Y/N) : y
Yes!
[work@www sh]$ sh hello.sh
Please input (Y/N) : l
Input Error
[work@www sh]$

2、case判断(shell条件判断)

如果if中判断层级比较多,建议使用case, 否则嵌套太多,代码易读性变差。 case的语法 case $变量名称 in “变量1”) do something ;; 每个结尾用两个分号 “变量2”) do something ;; *) 最后一个*代表其他值 do something ;; esac 看下面例子:
 

 

#!/bin/bash

case $1 in
"one")
echo "One"
;;
"two")
echo "Two"
;;
*)
echo "Usage $0 (one|two)"
;;
esac

exit 0

运行结果:
 

[work@ww sh]$ sh hello.sh
Usage hello.sh (one|two)
[work@www sh]$ sh hello.sh one
One
[work@www sh]$

shell if常用判断条件

if 判断
if语句就是很重要的一个。

基本结构: if语句块需要使用if结束
if condition
then
statements
elif condition
then statements
else
statements
fi

注意,if后面的判断条件,方括号和之间的判断语句左右各要有一个空格。

例子:
 

#判断字符是否相等
#!/bin/bash
system=`uname -s`

if [ $system = "Linux" ]    #方括号内部两边有空格,等号两边也需要空格
then
echo "Linux"
else
echo "Other system"
fi


常用类型的判断写法(类比字符串)
1、字符串判断

str1 = str2   当两个串有相同内容、长度时为真
str1 != str2  当串str1和str2不等时为真
-n str1  当串的长度大于0时为真(串非空)
-z str1  当串的长度为0时为真(空串)
str1 当串str1为非空时为真

2、数字的判断
 

int1 -eq int2两数相等为真
int1 -ne int2两数不等为真
int1 -gt int2int1大于int2为真
int1 -ge int2int1大于等于int2为真
int1 -lt int2int1小于int2为真
int1 -le int2int1小于等于int2为真


3、文件的判断

-r file 用户可读为真
-w file 用户可写为真
-x file 用户可执行为真
-f file 文件为正规文件为真
-d file 文件为目录为真
-c file 文件为字符特殊文件为真
-b file 文件为块特殊文件为真
-s file 文件大小非0时为真
-t file 当文件描述符(默认为1)指定的设备为终端时为真

4、复杂逻辑判断
-a     与
-o   或
! 非

tips:
* -eq -ne -lt -nt只能用于整数,不适用于字符串,字符串等于用赋值号=
* =放在别的地方是赋值,放在if [ ] 里就是字符串等于,shell里面没有==的,那是c语言的等于
* 整数条件表达式,大于,小于,shell里没有> 和< ,会被当作尖括号,只有-ge,-gt,-le,lt

shell条件判断:

-b file 若文件存在且是一个块特殊文件,则为真

-c file 若文件存在且是一个字符特殊文件,则为真

-d file 若文件存在且是一个目录,则为真

-e file 若文件存在,则为真

-f file 若文件存在且是一个规则文件,则为真

-g file 若文件存在且设置了SGID位的值,则为真

-h file 若文件存在且为一个符合链接,则为真

-k file 若文件存在且设置了"sticky"位的值

-p file 若文件存在且为一已命名管道,则为真

-r file 若文件存在且可读,则为真

-s file 若文件存在且其大小大于零,则为真

-u file 若文件存在且设置了SUID位,则为真

-w file 若文件存在且可写,则为真

-x file 若文件存在且可执行,则为真

-o file 若文件存在且被有效用户ID所拥有,则为真

-z string 若string长度为0,则为真

-n string 若string长度不为0,则为真

string1 = string2 若两个字符串相等,则为真

string1 != string2 若两个字符串不相等,则为真

int1 -eq int2 若int1等于int2,则为真

int1 -ne int2 若int1不等于int2,则为真

int1 -lt int2 若int1小于int2,则为真

int1 -le int2 若int1小于等于int2,则为真

int1 -gt int2 若int1大于int2,则为真

int1 -ge int2 若int1大于等于int2,则为真

!expr 若expr为假则复合表达式为真。expr可以是任何有效的测试表达式

expr1 -a expr2 若expr1和expr2都为真则整式为真

expr1 -o expr2 若expr1和expr2有一个为真则整式为真


特殊变量

$0 正在被执行命令的名字。对于shell脚本而言,这是被激活命令的路径

$n 该变量与脚本被激活时所带的参数相对应。n是正整数,与参数位置相对应($1,$2…)

$# 提供脚本的参数号

$* 所有这些参数都被双引号引住。若一个脚本接收两个参数,$*等于$1$2

$@ 所有这些参数都分别被双引号引住。若一个脚本接收到两个参数,$@等价于$1$2

$? 前一个命令执行后的退出状态

$$ 当前shell的进程号。对于shell脚本,这是其正在执行时的进程ID

$! 前一个后台命令的进程号

shell脚本if条件判断和判断条件总结

代码:

if list then 
do something here 
elif list then 
do another thing here 
else 
do something else here 
fi 

例子:


#!/bin/sh
SYSTEM=`uname -s`    #获取操作系统类型,我本地是linux
if [ $SYSTEM = "Linux" ] ; then     #如果是linux的话打印linux字符串
echo "Linux" 
elif [ $SYSTEM = "FreeBSD" ] ; then   
echo "FreeBSD" 
elif [ $SYSTEM = "Solaris" ] ; then 
echo "Solaris" 
else 
echo "What?" 
fi     #ifend

注意,[]中的条件判断。

1、字符串判断
 

str1 = str2当两个串有相同内容、长度时为真 
str1 != str2  当串str1和str2不等时为真 
-n str1  当串的长度大于0时为真(串非空) 
-z str1  当串的长度为0时为真(空串) 
str1   当串str1为非空时为真

2、数字的判断
 

int1 -eq int2两数相等为真 
int1 -ne int2两数不等为真 
int1 -gt int2int1大于int2为真 
int1 -ge int2int1大于等于int2为真 
int1 -lt int2int1小于int2为真 
int1 -le int2int1小于等于int2为真

3、文件的判断
 

-r file 用户可读为真 
-w file 用户可写为真 
-x file 用户可执行为真 
-f file 文件为正规文件为真 
-d file 文件为目录为真 
-c file 文件为字符特殊文件为真 
-b file 文件为块特殊文件为真 
-s file 文件大小非0时为真 
-t file 当文件描述符(默认为1)指定的设备为终端时为真
4、复杂逻辑判断
-a     与 
-o  或 
!非
结尾

附 表:
 

[ -a FILE ]  如果 FILE 存在则为真。  
[ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真。  
[ -c FILE ]  如果 FILE 存在且是一个字特殊文件则为真。  
[ -d FILE ]  如果 FILE 存在且是一个目录则为真。  
[ -e FILE ]  如果 FILE 存在则为真。  
[ -f FILE ]  如果 FILE 存在且是一个普通文件则为真。  
[ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。 [ -h FILE ]  如果 FILE 存在且是一个符号连接则为真。  
[ -k FILE ]  如果 FILE 存在且已经设置了粘制位则为真。  
[ -p FILE ]  如果 FILE 存在且是一个名字管道(F如果O)则为真。  
[ -r FILE ]  如果 FILE 存在且是可读的则为真。  
[ -s FILE ]  如果 FILE 存在且大小不为0则为真。  
[ -t FD ]  如果文件描述符 FD 打开且指向一个终端则为真。  
[ -u FILE ]  如果 FILE 存在且设置了SUID (set user ID)则为真。  
[ -w FILE ]  如果 FILE 如果 FILE 存在且是可写的则为真。  
[ -x FILE ]  如果 FILE 存在且是可执行的则为真。  
[ -O FILE ]  如果 FILE 存在且属有效用户ID则为真。  
[ -G FILE ]  如果 FILE 存在且属有效用户组则为真。  
[ -L FILE ]  如果 FILE 存在且是一个符号连接则为真。  
[ -N FILE ]  如果 FILE 存在 and has been mod如果ied since it was last read则为真。  
[ -S FILE ]  如果 FILE 存在且是一个套接字则为真。  
[ FILE1 -nt FILE2 ]  如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1 exists and FILE2 does not则为真。  
[ FILE1 -ot FILE2 ]  如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。  
[ FILE1 -ef FILE2 ]  如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。  
[ -o OPTIONNAME ]  如果 shell选项 “OPTIONNAME” 开启则为真。  
[ -z STRING ]  “STRING” 的长度为零则为真。  
[ -n STRING ] or [ STRING ]  “STRING” 的长度为非零 non-zero则为真。  
[ STRING1 == STRING2 ]  如果2个字符串相同。 “=” may be used instead of “==” for strict POSIX compliance则为真。  
[ STRING1 != STRING2 ]  如果字符串不相等则为真。 
[ STRING1 < STRING2 ]  如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。  
[ STRING1 > STRING2 ]  如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。  
[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容