1.2shell逻辑判断、文件属性判断、if特殊用法、case判断
2018-01-24 00:00
746 查看
20.5shell逻辑判断
20.6文件属性判断
20.7if特殊用法
20.8-20.9case判断
20.5shell逻辑判断
-lt:=little than 小于
-le:=little && equal 小于等于
-eq:=equal 等于
-ne:=no equal 不等于
-gt:=greater than 大于
-ge:=greater && equal 大于等于
在(( ))小括号中:
<,<=,==,!=,>,>=
注意: 使用双括号括起来
[root@tianqi-01 ~]# for i in `seq 1 5`; do echo $i; done
1
2
3
4
5
[root@tianqi-01 ~]#
有时候不用写脚本,1条命令足矣。
[root@tianqi-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@tianqi-01 ~]#
这条命令也可以不用分割,效果相同。
[root@tianqi-01 ~]# a=5
[root@tianqi-01 ~]# if [ $a -gt 3 ]
> then
> echo ok
> fi
ok
[root@tianqi-01 ~]# if [ $a -gt 3 ]; then echo ok; fi
ok
[root@tianqi-01 ~]#
[root@tianqi-01 shell]# vim if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo "ok"
fi
[root@tianqi-01 shell]# sh if1.sh
ok
[root@tianqi-01 shell]# vim if2.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
#注意[]内的空格
then
echo "ok"
else
echo "fault"
fi
[root@tianqi-01 shell]# sh if2.sh
ok
[root@tianqi-01 shell]# sh -x if2.sh
+ a=5
+ '[' 5 -gt 3 ']'
+ echo ok
ok
[root@tianqi-01 shell]#
[root@tianqi-01 shell]# vim if3.sh
a=5
if [ $a -lt 3 ]
then
echo "a<3"
elif [ $a -gt 6 ]
then
echo "a>6"
else
echo "Out of the zone"
fi
[root@tianqi-01 shell]# sh if3.sh
Out of the zone
[root@tianqi-01 shell]# sh -x if3.sh
+ a=5
+ '[' 5 -lt 3 ']'
+ '[' 5 -gt 6 ']'
+ echo 'Out of the zone'
Out of the zone
[root@tianqi-01 shell]#
条件A&&条件B:并且
条件A||条件B:或者
20.6文件属性判断
shell脚本中if经常用于判断文档的属性,比如判断是普通文件还是目录文件,判断文件是否有读、写、执行权限等。if常用的选项有以下几个:
-e:判断文件或目录是否存在
-d:判断是不是目录文件以及是否存在
-f:判断是不是普通文件以及是否存在
-r:判断是否有读权限
-w:判断是否有写权限
-x:判断是否有执行权限
if [ -e filename ]
then
commond
fi
以上命令可简化为:
[ -e filename ] && commond
&&前后的命都执行
或:
[ -e filename ] || commond
当 || 前面的执行不成功时再执行后面的命令
如果某文件不存在:
if [ ! -e filename ]
then
commond
fi
“!”等同于取反。
[root@localhost shell]# vim file1.sh
#!/bin/bash
a="/tmp/aminglinux"
if [ -f $a ]
then
echo $a exist
else
touch $a
f
[root@localhost tmp]# sh -x file1.sh
+ a=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ touch /tmp/aminglinux
20.7if特殊用法
if [ -z "$a" ]:表示当变量a的值为空时会怎样
if [ -n "$a" ]:表示当变量a的值不为空时会怎样
-z和-n为相反的两个反条件。
[root@tianqi-01 shell]# vim if.sh
#!/bin/bash
n=`wc -l /tmp/test.txt`
if [ $n -gt 20 ]
then
echo 1
else
echo 0
fi
在该脚本中无语法错误,只是我们预设/tmp/test.txt是存在的,如果该文件不存在,该脚本执行的时候就会报错:
[root@tianqi-01 shell]# sh if.sh
wc: /tmp/test.txt: No such file or directory
if.sh: line 5: [: -gt: unary operator expected
if.sh: line 13: $'\302\240\302\240\302\240\302\240echo': command not found
[root@tianqi-01 shell]#
所以,为了避免这种错误的发生,需要将脚本写的更加严谨,需要在执行“if [ $n -gt 20 ]”之前先确认文件“/tmp/test.txt”是否存在:
[root@tianqi-01 shell]# vim exit.sh
#!/bin/bash
n=`wc -l /tmp/test.txt`
if [ -z $n ]
then
echo "error"
exit
elif [ $n -lt 20 ]
then
echo 1
else
echo 0
fi
即,如果变量n为空则显示error并退出该脚本。
[root@tianqi-01 shell]# sh exit.sh
wc: /tmp/test.txt: No such file or directory
error
即,当该文件不存在的时候就会退出执行,不会提示存在语法错误。(该脚本存在逻辑错误,只做效果演示用)
注意: 在该表达式中引用变量时要用双引号引起来。
判断某参数存在:
[root@tianqi-01 shell]# vim exit1.sh
#!/bin/bash
if grep -wq 'user1' /etc/passwd
then
echo "user1 exit"
fi
[root@tianqi-01 shell]# sh exit1.sh
user1 exit
判断某参数不存在
[root@tianqi-01 shell]# vim exit1.sh
#!/bin/bash
if ! grep -wq 'user1' /etc/passwd
then
echo "user1 exit"
fi
[root@tianqi-01 shell]# sh exit1.sh
说明: grep中-w选项=Word,表示过滤一个单词;-q,表示不打印过滤的结果。判断某参数不存在时使用!表示取反。
20.8-20.9case判断
格式:
case 变量名 in
value1)
commond1
;;
value2)
commod2
;;
value3)
commod3
;;
*)
commod3
;;
esac
在case中,可以在条件中使用“|”,表示或的意思,如:
2|3)
command
;;
[root@tianqi-01 shell]# vim case1.sh
#!/bin/bash
#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number:" n
#read 让用户输出一些字符串,赋值给最后一个变量,这里的赋值是“n”
if [ -z "$n" ] //变量n为空
then
echo "Please input a number."
exit 1
#“exit 1”表示执行该部分命令后的返回值 ,即,命令执行完后使用echo $?的值
fi
n1=`echo $n|sed 's/[0-9]//g'`
#判断用户输入的字符是否为纯数字 ,如果是数字,则将其替换为空,赋值给$n1
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
#判断$n1不为空时(即$n不是纯数字)再次提示用户输入数字并退出
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
#tag的作用是为判断条件设定标签,方便后面引用
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number rang is 0-100."
;;
esac
说明: 该脚本的作用是输入考试成绩,判断考试成绩的等级。
[root@tianqi-01 shell]# sh case1.sh
Please input a number:95
oook
[root@tianqi-01 shell]# sh case1.sh
Please input a number:82
ook
[root@tianqi-01 shell]# sh case1.sh
Please input a number:63
ok
[root@tianqi-01 shell]# sh case1.sh
Please input a number:35
not ok
exit(0):正常运行程序并退出程序;
exit(1):非正常运行导致退出程序;
exit 0 可以告知你的程序的使用者:你的程序是正常结束的。如果 exit 非 0 值,那么你的程序的使用者通常会认为你的程序产生了一个错误。
在 shell 中调用完你的程序之后,用 echo $? 命令就可以看到你的程序的 exit 值。在 shell 脚本中,通常会根据上一个命令的 $? 值来进行一些流程控制。
友情链接:阿铭linux
20.6文件属性判断
20.7if特殊用法
20.8-20.9case判断
20.5shell逻辑判断
逻辑表达式
在[ ]中括号中:-lt:=little than 小于
-le:=little && equal 小于等于
-eq:=equal 等于
-ne:=no equal 不等于
-gt:=greater than 大于
-ge:=greater && equal 大于等于
在(( ))小括号中:
<,<=,==,!=,>,>=
注意: 使用双括号括起来
[root@tianqi-01 ~]# for i in `seq 1 5`; do echo $i; done
1
2
3
4
5
[root@tianqi-01 ~]#
有时候不用写脚本,1条命令足矣。
[root@tianqi-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@tianqi-01 ~]#
这条命令也可以不用分割,效果相同。
格式1
if 条件 ;then commond;fi[root@tianqi-01 ~]# a=5
[root@tianqi-01 ~]# if [ $a -gt 3 ]
> then
> echo ok
> fi
ok
[root@tianqi-01 ~]# if [ $a -gt 3 ]; then echo ok; fi
ok
[root@tianqi-01 ~]#
[root@tianqi-01 shell]# vim if1.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo "ok"
fi
[root@tianqi-01 shell]# sh if1.sh
ok
格式2
if 条件1;then commond1;else commond2;fi[root@tianqi-01 shell]# vim if2.sh
#!/bin/bash
a=5
if [ $a -gt 3 ]
#注意[]内的空格
then
echo "ok"
else
echo "fault"
fi
[root@tianqi-01 shell]# sh if2.sh
ok
[root@tianqi-01 shell]# sh -x if2.sh
+ a=5
+ '[' 5 -gt 3 ']'
+ echo ok
ok
[root@tianqi-01 shell]#
格式3
if 条件1;then commond1;elif 条件2;then commond2;else commond3;fi[root@tianqi-01 shell]# vim if3.sh
a=5
if [ $a -lt 3 ]
then
echo "a<3"
elif [ $a -gt 6 ]
then
echo "a>6"
else
echo "Out of the zone"
fi
[root@tianqi-01 shell]# sh if3.sh
Out of the zone
[root@tianqi-01 shell]# sh -x if3.sh
+ a=5
+ '[' 5 -lt 3 ']'
+ '[' 5 -gt 6 ']'
+ echo 'Out of the zone'
Out of the zone
[root@tianqi-01 shell]#
关系
各个条件之间的关系可以使用逻辑连接符:条件A&&条件B:并且
条件A||条件B:或者
20.6文件属性判断
shell脚本中if经常用于判断文档的属性,比如判断是普通文件还是目录文件,判断文件是否有读、写、执行权限等。if常用的选项有以下几个:
-e:判断文件或目录是否存在
-d:判断是不是目录文件以及是否存在
-f:判断是不是普通文件以及是否存在
-r:判断是否有读权限
-w:判断是否有写权限
-x:判断是否有执行权限
格式
如果某文件存在:if [ -e filename ]
then
commond
fi
以上命令可简化为:
[ -e filename ] && commond
&&前后的命都执行
或:
[ -e filename ] || commond
当 || 前面的执行不成功时再执行后面的命令
如果某文件不存在:
if [ ! -e filename ]
then
commond
fi
“!”等同于取反。
[root@localhost shell]# vim file1.sh
#!/bin/bash
a="/tmp/aminglinux"
if [ -f $a ]
then
echo $a exist
else
touch $a
f
[root@localhost tmp]# sh -x file1.sh
+ a=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ touch /tmp/aminglinux
20.7if特殊用法
if [ -z "$a" ]:表示当变量a的值为空时会怎样
if [ -n "$a" ]:表示当变量a的值不为空时会怎样
-z和-n为相反的两个反条件。
[root@tianqi-01 shell]# vim if.sh
#!/bin/bash
n=`wc -l /tmp/test.txt`
if [ $n -gt 20 ]
then
echo 1
else
echo 0
fi
在该脚本中无语法错误,只是我们预设/tmp/test.txt是存在的,如果该文件不存在,该脚本执行的时候就会报错:
[root@tianqi-01 shell]# sh if.sh
wc: /tmp/test.txt: No such file or directory
if.sh: line 5: [: -gt: unary operator expected
if.sh: line 13: $'\302\240\302\240\302\240\302\240echo': command not found
[root@tianqi-01 shell]#
所以,为了避免这种错误的发生,需要将脚本写的更加严谨,需要在执行“if [ $n -gt 20 ]”之前先确认文件“/tmp/test.txt”是否存在:
[root@tianqi-01 shell]# vim exit.sh
#!/bin/bash
n=`wc -l /tmp/test.txt`
if [ -z $n ]
then
echo "error"
exit
elif [ $n -lt 20 ]
then
echo 1
else
echo 0
fi
即,如果变量n为空则显示error并退出该脚本。
[root@tianqi-01 shell]# sh exit.sh
wc: /tmp/test.txt: No such file or directory
error
即,当该文件不存在的时候就会退出执行,不会提示存在语法错误。(该脚本存在逻辑错误,只做效果演示用)
注意: 在该表达式中引用变量时要用双引号引起来。
判断某参数存在:
[root@tianqi-01 shell]# vim exit1.sh
#!/bin/bash
if grep -wq 'user1' /etc/passwd
then
echo "user1 exit"
fi
[root@tianqi-01 shell]# sh exit1.sh
user1 exit
判断某参数不存在
[root@tianqi-01 shell]# vim exit1.sh
#!/bin/bash
if ! grep -wq 'user1' /etc/passwd
then
echo "user1 exit"
fi
[root@tianqi-01 shell]# sh exit1.sh
说明: grep中-w选项=Word,表示过滤一个单词;-q,表示不打印过滤的结果。判断某参数不存在时使用!表示取反。
20.8-20.9case判断
格式:
case 变量名 in
value1)
commond1
;;
value2)
commod2
;;
value3)
commod3
;;
*)
commod3
;;
esac
在case中,可以在条件中使用“|”,表示或的意思,如:
2|3)
command
;;
[root@tianqi-01 shell]# vim case1.sh
#!/bin/bash
#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number:" n
#read 让用户输出一些字符串,赋值给最后一个变量,这里的赋值是“n”
if [ -z "$n" ] //变量n为空
then
echo "Please input a number."
exit 1
#“exit 1”表示执行该部分命令后的返回值 ,即,命令执行完后使用echo $?的值
fi
n1=`echo $n|sed 's/[0-9]//g'`
#判断用户输入的字符是否为纯数字 ,如果是数字,则将其替换为空,赋值给$n1
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
#判断$n1不为空时(即$n不是纯数字)再次提示用户输入数字并退出
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
#tag的作用是为判断条件设定标签,方便后面引用
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number rang is 0-100."
;;
esac
说明: 该脚本的作用是输入考试成绩,判断考试成绩的等级。
[root@tianqi-01 shell]# sh case1.sh
Please input a number:95
oook
[root@tianqi-01 shell]# sh case1.sh
Please input a number:82
ook
[root@tianqi-01 shell]# sh case1.sh
Please input a number:63
ok
[root@tianqi-01 shell]# sh case1.sh
Please input a number:35
not ok
知识点 1
shell 中 exit0 exit1 的区别:exit(0):正常运行程序并退出程序;
exit(1):非正常运行导致退出程序;
exit 0 可以告知你的程序的使用者:你的程序是正常结束的。如果 exit 非 0 值,那么你的程序的使用者通常会认为你的程序产生了一个错误。
在 shell 中调用完你的程序之后,用 echo $? 命令就可以看到你的程序的 exit 值。在 shell 脚本中,通常会根据上一个命令的 $? 值来进行一些流程控制。
友情链接:阿铭linux
相关文章推荐
- 20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断
- shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
- 68.if逻辑判断以及判断文件目录属性 if特殊用法 case判断
- shell逻辑判断、文件属性判断、if特殊用法、case判断
- shell逻辑判断、文件属性判断、if特殊用法、case判断
- 20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断20.7 if 特殊用法 20.8/20.9 case判断
- shell脚本中的逻辑判断,文件目录属性判断,if特殊用法,case语句
- 十六周三次课 2018.02.06 shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的逻辑判断、文件目录属性判断、 if特殊用法、case判断
- shell脚本中的逻辑判断-文件目录属性判断-if特殊用法-case判断
- linux学习第六十四篇:Shell脚本中的逻辑判断,文件目录属性判断, if特殊用法,case判断
- linux的shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
- 20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)
- 【shell编程】shell脚本中的逻辑判断、文件目录属性判断、if特殊用法和case判断
- Shell编程(逻辑判断、文件目录属性判断、if特殊用法、case判断)
- shell脚本中的逻辑判断,文件目录属性判断,if特殊用法,case判断
- shell脚本结构、date命令、变量 shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
- shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的if逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的逻辑判断文件目录属性判断 if特殊用法 case判断