格式:linux
if 判断语句;then command fi
实例:shell
[root@zlinux-01 shell]# vim if01.sh //判断数值大小第一种方法用[],注意先后空格 #! /bin/bash a=5 if [ $a -gt 3 ];then echo "这个数字大于3" fi #-gt:大于,-lt:小于,-ge:大于或等于,-le:小于或等于,-eq:等于,-ne:不等于 [root@zlinux-01 shell]# sh if01.sh 这个数字大于3 [root@zlinux-01 shell]# vim if02.sh //判断数值大小的第二种方式:(()) #! /bin/bash a=5 if ((a>3));then echo "这个数字大于3" fi #((a>3)),双括号是shell中的特有格式,只用一个括号或者不用都会报错 [root@zlinux-01 shell]# sh if02.sh 这个数字大于3
格式:vim
if 判断语句;then command else command fi
实例:bash
[root@zlinux-01 shell]# vim ifelse.sh #! /bin/bash read -p "请输入分数:" a if [ $a -lt 60 ];then echo "你没有经过测试!" else echo "恭喜你,顺利经过测试!" fi #read命令用于和用户交互,它把用户输入的字符串做为变量值 [root@zlinux-01 shell]# sh ifelse.sh 请输入分数:60 恭喜你,顺利经过测试! [root@zlinux-01 shell]# sh ifelse.sh 请输入分数:59 你没有经过测试!
格式:ide
if 判断语句1;then command elif 判断语句2;then command else command fi
实例:测试
[root@zlinux-01 shell]# vim elif.sh #! /bin/bash read -p "请输入分数:" a if (($a<60));then echo "未经过测试。" elif ((a>=60))&&(($a<85));then echo "经过测试,成绩良好。" else echo "经过测试,成绩优秀!" fi # &&表示而且,固然也能够用||表示或者 [root@zlinux-01 shell]# sh elif.sh 请输入分数:60 经过测试,成绩良好。 [root@zlinux-01 shell]# sh elif.sh 请输入分数:34 未经过测试。 [root@zlinux-01 shell]# sh elif.sh 请输入分数:99 经过测试,成绩优秀!
shell中if嗨常常用于判断文档属性。经常使用选项有:code
-e:判断文件或目录是否存在;
-d:判断是否是目录以及是否存在;
-f:判断是不是文件以及是否存在;
-r:判断是否有读权限;
-w;判断是否有写权限;
-x:判断是否有执行权限。文档
格式:字符串
if [ -e filename ];then command fi
实例:input
[root@zlinux-01 shell]# if [ -d /home/ ]; then echo ok; fi ok [root@zlinux-01 shell]# if [ -f /home/ ]; then echo ok; fi //home是目录不是文件,因此没有输出ok [root@zlinux-01 shell]# touch /root/test.txt [root@zlinux-01 shell]# if [ -f /root/test.txt ]; then echo ok; fi ok [root@zlinux-01 shell]# if [ -r /root/test.txt ]; then echo ok; fi ok [root@zlinux-01 shell]# if [ -w /root/test.txt ]; then echo ok; fi ok [root@zlinux-01 shell]# if [ -x /root/test.txt ]; then echo ok; fi [root@zlinux-01 shell]# if [ -e /root/test1.txt ]; then echo ok; fi [root@zlinux-01 shell]# if [ -d /root/test.txt ]; then echo ok; fi
格式:
case 变量 in value1) command ;; value2) command ;; value3) command ;; *) command ;; esac
不限制value的个数,*表示其余值
实例:
[root@zlinux-01 shell]# vim case.sh #! /bin/bash read -p "请输入一个数字:" n a=$[$n%2] case $a in 1) echo "这数字是奇数" ;; 0) echo "这数字是偶数" ;; *) echo "这个不是数字" ;; esac [root@zlinux-01 shell]# sh case.sh 请输入一个数字:3 这数字是奇数 [root@zlinux-01 shell]# sh case.sh 请输入一个数字:2 这数字是偶数
[root@zlinux-01 shell]# vim test01.sh #!/bin/bash read -p "Please input a number: " n if [ -z "$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 range is 0-100." ;; esac [root@zlinux-01 shell]# sh test01.sh Please input a number: aa Please input a number. [root@zlinux-01 shell]# sh test01.sh Please input a number: 78 ok [root@zlinux-01 shell]# sh test01.sh Please input a number: 90 oook [root@zlinux-01 shell]# sh test01.sh Please input a number: 11 not ok