shell脚本中的逻辑判断,文件目录属性判断,if特殊用法,case判断
2018-02-06 00:00
567 查看
shell脚本中的逻辑判断
格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi
格式3:if …; then … ;elif …; then …; else …; fi
逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
for语句
![](https://static.oschina.net/uploads/space/2018/0206/230824_1xAF_3708406.png)
格式1:if 条件 ; then 语句; fi
![](https://static.oschina.net/uploads/space/2018/0207/072751_Fjpb_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/072809_UjqC_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/072735_gefb_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/072828_qB9Y_3708406.png)
格式2:if 条件; then 语句; else 语句; fi
![](https://static.oschina.net/uploads/space/2018/0207/073057_wyoc_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/073138_yB5H_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/073150_D3Pu_3708406.png)
格式3:if …; then … ;elif …; then …; else …; fi
![](https://static.oschina.net/uploads/space/2018/0207/073356_SeLy_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/073830_lER3_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/073840_GJoH_3708406.png)
逻辑判断表达式
-lt:< 小于
-le:<= 小于等于
-eq:==等于
-ne:!=不等于
-gt:>大于
-ge:>=大于等于
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
文件目录属性判断
[ -f file ]判断是否是普通文件,且存在[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
[ -f file ]判断是否是普通文件,且存在
![](https://static.oschina.net/uploads/space/2018/0207/075349_P1wn_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/075827_wbXC_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/075908_yh6w_3708406.png)
判断是否是目录,且存在
![](https://static.oschina.net/uploads/space/2018/0207/080000_ZTfI_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/075949_Nugb_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080019_Ze4n_3708406.png)
判断文件或目录是否存在
![](https://static.oschina.net/uploads/space/2018/0207/080331_z3Gn_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080309_n8T8_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080349_zghA_3708406.png)
[ -r file ] 判断文件是否可读
![](https://static.oschina.net/uploads/space/2018/0207/080443_j1II_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080418_9Tja_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080454_nK3s_3708406.png)
[ -w file ] 判断文件是否可写
![](https://static.oschina.net/uploads/space/2018/0207/080542_42yZ_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080531_MV3i_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080550_1R3l_3708406.png)
[ -x file ] 判断文件是否可执行
![](https://static.oschina.net/uploads/space/2018/0207/080638_QNTV_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080628_UvkX_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/080659_k2e7_3708406.png)
并且&& 前一条命令执行成功才会继续执行之后的命令
![](https://static.oschina.net/uploads/space/2018/0207/081118_Aywd_3708406.png)
或者 || 前面命令不成功时,执行后面的命令
![](https://static.oschina.net/uploads/space/2018/0207/081002_fhAh_3708406.png)
if特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
-z 表示变量为空
![](https://static.oschina.net/uploads/space/2018/0207/082344_0xXa_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/082320_gLXf_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/082430_vDhC_3708406.png)
-n表示变量不为空
![](https://static.oschina.net/uploads/space/2018/0207/083002_t8gh_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/083019_ZMdf_3708406.png)
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
![](https://static.oschina.net/uploads/space/2018/0207/083410_GKTA_3708406.png)
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
case判断
case格式变量名 in value1) command ;; value2) command ;; *) commond ;; esac
在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3) command ;;
脚本案例
目的:用户输入一个数字,然后用脚本去判断这个数字的范围
![](https://static.oschina.net/uploads/space/2018/0207/084730_Sfed_3708406.png)
#!/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'` //确定变量n是否为纯数字,如果是数字,则清空 if [ ! -z "$n1" ] then echo "Please input a number." exit 1 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 case $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "ook" ;; 4) echo "oook" ;; *) echo "The input value exceeds the calculation range.The number range is 0-100." ;; esac
![](https://static.oschina.net/uploads/space/2018/0207/091317_wpwe_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/091346_G8ci_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/091358_Fnbu_3708406.png)
![](https://static.oschina.net/uploads/space/2018/0207/091419_kCil_3708406.png)
相关文章推荐
- shell脚本中的逻辑判断,文件目录属性判断,if特殊用法,case语句
- 【shell编程】shell脚本中的逻辑判断、文件目录属性判断、if特殊用法和case判断
- 十六周三次课 2018.02.06 shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的逻辑判断-文件目录属性判断-if特殊用法-case判断
- shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的逻辑判断、文件目录属性判断、 if特殊用法、case判断
- linux的shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的if逻辑判断、文件目录属性判断、if特殊用法、case判断
- shell脚本中的逻辑判断文件目录属性判断 if特殊用法 case判断
- shell脚本结构、date命令、变量 shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
- linux学习第六十四篇:Shell脚本中的逻辑判断,文件目录属性判断, if特殊用法,case判断
- 20.5 shell脚本中的逻辑判断 文件目录属性判断 if特殊用法
- shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
- 20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断20.7 if 特殊用法 20.8/20.9 case判断
- 20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断
- Shell编程(逻辑判断、文件目录属性判断、if特殊用法、case判断)
- shell逻辑判断、文件属性判断、if特殊用法、case判断
- 20.5 Shell脚本中的逻辑判断;20.6 文件目录属性判断;20.7 if特殊用法;20.8 20.9 cace判断(上下)
- shell逻辑判断、文件属性判断、if特殊用法、case判断
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课