本文共 4531 字,大约阅读时间需要 15 分钟。
1.if then fi
[root@weixing01 shell]# cat if1.sh#!/bin/basha=5if [ $a -gt 3 ]then echo okfi
2.if then else fi:
root@weixing01 shell]# sh -x if2.sh + a=1+ '[' 1 -gt 3 ']'+ echo nooknook[root@weixing01 shell]# cat if2.sh #!/bin/basha=1if [ $a -gt 3 ]then echo okelse echo nookfi
3.if then elif then else fi:
[root@weixing01 shell]# cat if3.sh #!/bin/basha=4if [ $a -gt 4 ]then echo ">1"elif [ $a -lt 4 ]then echo "<4"else echo "=4"fi
[root@weixing01 shell]# sh -x if3.sh + a=3+ '[' 3 -gt 4 ']'+ '[' 3 -lt 4 ']'+ echo '<4'<4[root@weixing01 shell]# vi if3.sh [root@weixing01 shell]# sh -x if3.sh + a=4+ '[' 4 -gt 4 ']'+ '[' 4 -lt 4 ']'+ echo =4=4[root@weixing01 shell]# cat if3.sh #!/bin/basha=4
4.注意【】两侧都需要有空格,-gt 大于 -lt 小于 -eq等于 两侧都需要空格 -ge大于等于 -le小于等于 noeq 不等于
5.if逻辑判断支持||和&&
1.-f file 判断是否是普通文件,且存在
[root@weixing01 shell]# sh -x file1.sh + f=/tmp/aminglinux+ '[' -f /tmp/aminglinux ']'+ touch /tmp/aminglinux[root@weixing01 shell]# cat file1.sh #!/bin/bashf="/tmp/aminglinux"if [ -f $f ]then echo $f existelse touch $ffi[root@weixing01 shell]# sh -x file1.sh + f=/tmp/aminglinux+ '[' -f /tmp/aminglinux ']'+ echo /tmp/aminglinux exist/tmp/aminglinux exist
2.-d file 判断是否是目录且存在:
[root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux+ '[' -d /tmp/aminglinux ']'+ touch /tmp/aminglinux[root@weixing01 shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -d $f ]then echo $d existelse touch $ffi
3.-e判断文件或者目录是否存在:
[root@weixing01 shell]# vi file2.sh [root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux+ '[' -e /tmp/aminglinux ']'+ echo existexist
4.-r判断文件是否可读
[root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux+ '[' -r /tmp/aminglinux ']'+ echo /tmp/aminglinux readable/tmp/aminglinux readable[root@weixing01 shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -r $f ]then echo $f readablefi
5.-w判断文件是否可写
[root@weixing01 shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -w $f ]then echo $f writeablefi[root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux+ '[' -w /tmp/aminglinux ']'+ echo /tmp/aminglinux writeable/tmp/aminglinux writeable
6.-x判断是否可执行:不可执行,没有输出
[root@weixing01 shell]# ls -l /tmp/aminglinux -rw-r--r-- 1 root root 0 4月 18 21:36 /tmp/aminglinux[root@weixing01 shell]# vi file2.sh [root@weixing01 shell]# cat file2.sh #!/bin/bashf="/tmp/aminglinux"if [ -x $f ]then echo $f exeablefi[root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux+ '[' -x /tmp/aminglinux ']'
1.判断变量是否为空:
++ wc -l /tmp/lalalwc: /tmp/lalal: 没有那个文件或目录+ n=+ '[' -gt 100 ']'if4.sh: 第 3 行:[: -gt: 期待一元表达式[root@weixing01 shell]# vi if4.sh[root@weixing01 shell]# sh -x if4.sh ++ wc -l /tmp/lalalwc: /tmp/lalal: 没有那个文件或目录+ n=+ '[' -z '' ']'+ echo errorerror[root@weixing01 shell]# cat if4.sh #!/bin/bashn=`wc -l /tmp/lalal`if [ -z "$n" ]then echo errorelif [ $n -gt 100 ]then echo aldkjglkafi
2.-n判断是否不为空:
[root@weixing01 shell]# ls01.sh file1.sh file2.sh if1.sh if2.sh if3.sh if4.sh[root@weixing01 shell]# if [ -n 01.sh ]; then echo ok; fiok
3.-q 文件中含有字符时会怎样:
[root@weixing01 shell]# if grep -wq 'weixing01' /etc/passwd; then echo "sdjfk"; fisdjfk
1.编写脚本:
[root@weixing01 shell]# cat case1.sh #!/bin/bashread -p "Please input a number: " nif [ -z "$n" ]then echo "Please input a number." exit 1fin1=`echo $n|sed 's/[0-9]//g'`if [ -n "$n1" ]then echo "Please input a number." exit 1fiif [ $n -lt 60 ] && [ $n -ge 0 ]then tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0ficase $tag in 1) echo "not ok" ;; 2) echo "ok" ;; 3) echo "ook" ;; 4) echo "oook" ;; *) echo "The number range is 0-100." ;; esac
[root@weixing01 shell]# sh -x case1.sh + read -p 'Please input a number: ' nPlease input a number: 101+ '[' -z 101 ']'++ echo 101++ sed 's/[0-9]//g'+ n1=+ '[' -n '' ']'+ '[' 101 -lt 60 ']'+ '[' 101 -ge 60 ']'+ '[' 101 -lt 80 ']'+ '[' 101 -ge 80 ']'+ '[' 101 -lt 90 ']'+ '[' 101 -ge 90 ']'+ '[' 101 -le 100 ']'+ tag=0+ case $tag in+ echo 'The number range is 0-100.'The number range is 0-100.
[root@weixing01 shell]# sh -x case1.sh + read -p 'Please input a number: ' nPlease input a number: 78+ '[' -z 78 ']'++ echo 78++ sed 's/[0-9]//g'+ n1=+ '[' -n '' ']'+ '[' 78 -lt 60 ']'+ '[' 78 -ge 60 ']'+ '[' 78 -lt 80 ']'+ tag=2+ case $tag in+ echo okok
转载于:https://blog.51cto.com/13517254/2105139