扬中市建设局网站,广州网站推广服务,网站用axure做的rp格式,网页设计个人网站设计概念
shell的条件测试目的是得出真和假。 shell 提供的条件测试语法 test 命令 [] 中括号命令 语法*#xff1a;
test条件测试
test命令用来评估一个表达式#xff0c;他的结果是真#xff0c;还是假#xff0c;如果条件为真#xff0c;那么命令执行状态结果就为0
test条件测试
test命令用来评估一个表达式他的结果是真还是假如果条件为真那么命令执行状态结果就为0否则就是不为0通过$?取值。 test命令参数 检测给定文件名是否存在 -e 判断该文件是否存在普通文件目录存在就为真否则为假 检测给定文件名的类型 -f 判断该文件名是否为文件 -d 判断该文件名是否为目录 -b 判断该文件名是否为块设备 -c 判断该文件名是否为字符设备 -s 判断该文件名是否为socket -p 判断该文件名是否为管道FIFO -L 判断该文件名是否为连接 检测文件的权限 -r 判断该文件名是否可读 -w 判断该文件名是否可写 -x 判断该文件名是否可运行 -u 判断该文件名是否有SUID属性 -g 判断该文件名是否有SGID属性 -k 判断该文件名是否有Sticky bit属性 -s 判断该文件名是否为空白文件 文件比较 如test file1 -nt file2 -nt newer than判断file1是否比file2新 -otolder than判断file1是否比file2旧 -ef 判断file1与file2是否为同一个文件可用在判断hard link的判定上主要意义在判定两个文件是否均指向同一个inode 整数之间判定 -eq 两数值相等 -ne 两数值不等 -gt n1大于n2 greater than -lt n1小于n2less than -ge n1大于等于n2 greater than or equal -le n1小于等于n2less than or equal 判定字符串数据 -z 判断字符串是否为0若字符串为空则为true -n 判断字符串是否为非0若字符串为非空则为true 判断str1与str2是否相等相等则为true ! 判断str1与str2是否不想等不相等则为true 多重条件判断 -a and两状态同时成立则返回true -o or两状态任意一个成立则返回true ! 反向状态 test命令实践
-e演示
xiao123xiao123:~/Downloads/shscripts$ test -e del_data.sh
xiao123xiao123:~/Downloads/shscripts$ echo $?
0
xiao123xiao123:~/Downloads/shscripts$ test -e del_data.sh12
xiao123xiao123:~/Downloads/shscripts$ echo $?
1
xiao123xiao123:~/Downloads/shscripts$
xiao123xiao123:~/Downloads/shscripts$ test -e del_data.sh echo 文件已存在
文件已存在
xiao123xiao123:~/Downloads/shscripts$ test -e del_data.sh12 echo 文件已存在
xiao123xiao123:~/Downloads/shscripts$ test -e del_data.sh12 || echo 文件不存在
文件不存在
xiao123xiao123:~/Downloads/shscripts$
xiao123xiao123:~/Downloads/shscripts$ test -e hello echo 该文件/目录已存在不再执行创建动作 || mkdir hello
xiao123xiao123:~/Downloads/shscripts$ test -e hello echo 该文件/目录已存在不再执行创建动作 || mkdir hello
该文件/目录已存在不再执行创建动作
xiao123xiao123:~/Downloads/shscripts$-f演示
xiao123xiao123:~/Downloads/shscripts$ ls
calculation.sh chaochao_5_finished.png index.html.11 index.html.2 index.html.28 index.html.6
chaochao_1_finished.png chaochao_5.jpg index.html.12 index.html.20 index.html.29 index.html.7
chaochao_1.jpg del_data.sh index.html.13 index.html.21 index.html.3 index.html.8
chaochao_2_finished.png expr_test1.sh index.html.14 index.html.22 index.html.30 index.html.9
chaochao_2.jpg expr_test.sh index.html.15 index.html.23 index.html.31 let_test.sh
chaochao_3_finished.png hello index.html.16 index.html.24 index.html.32
chaochao_3.jpg index.html index.html.17 index.html.25 index.html.33
chaochao_4_finished.png index.html.1 index.html.18 index.html.26 index.html.4
chaochao_4.jpg index.html.10 index.html.19 index.html.27 index.html.5
xiao123xiao123:~/Downloads/shscripts$ test -f hello echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$-d演示
xiao123xiao123:~/Downloads/shscripts$ ls
calculation.sh chaochao_5_finished.png index.html.11 index.html.2 index.html.28 index.html.6
chaochao_1_finished.png chaochao_5.jpg index.html.12 index.html.20 index.html.29 index.html.7
chaochao_1.jpg del_data.sh index.html.13 index.html.21 index.html.3 index.html.8
chaochao_2_finished.png expr_test1.sh index.html.14 index.html.22 index.html.30 index.html.9
chaochao_2.jpg expr_test.sh index.html.15 index.html.23 index.html.31 let_test.sh
chaochao_3_finished.png hello index.html.16 index.html.24 index.html.32
chaochao_3.jpg index.html index.html.17 index.html.25 index.html.33
chaochao_4_finished.png index.html.1 index.html.18 index.html.26 index.html.4
chaochao_4.jpg index.html.10 index.html.19 index.html.27 index.html.5
xiao123xiao123:~/Downloads/shscripts$ test -d hello echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$-z/-n演示
xiao123xiao123:~/Downloads/shscripts$ test -d hello echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$ test -z echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$ test -z echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$ test -n echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$ test -n echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$中括号条件测试
脚本中经常进行条件测试用的最多的都是中括号。 test和[]的作用是一样的。 注意点中括号前后都要有空格[ ] [ -n “${filename}” ] 注意在条件测试中使用变量必须要添加双引号 xiao123xiao123:~/Downloads/shscripts$ filedel_data.sh
xiao123xiao123:~/Downloads/shscripts$ [ -f ${file} ] echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$ filedel_data.sh12
xiao123xiao123:~/Downloads/shscripts$ [ -f ${file} ] echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$双中括号条件测试
双中括号增加了正则表达式的支持其他与中括号相同。
变量测试 把字符串写入变量中 对变量测试必须要加双引号 xiao123xiao123:~/Downloads/shscripts$ file1鸡你太美.jpg
xiao123xiao123:~/Downloads/shscripts$ [[ -f ${file1} ]] echo 我是两年半的练习生鸡你太美 || echo 我是个好人
我是个好人
xiao123xiao123:~/Downloads/shscripts$ [[ -f ${file1} ]] echo 我是两年半的练习生鸡你太美 || echo 我是个好人
我是个好人
xiao123xiao123:~/Downloads/shscripts$ touch 鸡你太美.jpg
xiao123xiao123:~/Downloads/shscripts$ [[ -f ${file1} ]] echo 我是两年半的练习生鸡你太美 || echo 我是个好人
我是两年半的练习生鸡你太美
xiao123xiao123:~/Downloads/shscripts$字符串测试
比较两个字符串变量的值是否相等不等的情况。 判断是否相等 ! 判断不相等 ! 取结果的反义颠倒黑白 注意: 对于字符串变量的比较一定要给变量添加双引号 使用等于号判断左右两边必须有空格 演示
xiao123xiao123:~/Downloads/shscripts$ [ ! -f 糖果超甜组合.txt ] echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$ [ -f 糖果超甜组合.txt ] echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$数值比较测试
操作方式
在中括号以及test中数值比较的用法 在中括号中使用数学比较符号请添加转义符号 xiao123xiao123:~/Downloads/shscripts$ [ 2 1 ] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [ 1 2 ] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [ 1 \ 2 ] echo yes || echo no
no
xiao123xiao123:~/Downloads/shscripts$
xiao123xiao123:~/Downloads/shscripts$ [ 2 \ 2 ] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [ 2 ! 2 ] echo yes || echo no
no #不等于可以不用添加转义
xiao123xiao123:~/Downloads/shscripts$ [ 3 ! 2 ] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [ 3 \! 2 ] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [ 3 \!\ 2 ] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [ 3 \!\ 3 ] echo yes || echo no
no
xiao123xiao123:~/Downloads/shscripts$双中括号 对中括号的补充双中括号还支持正则处理。 在双中括号中不要转义字符。
xiao123xiao123:~/Downloads/shscripts$ [[ 5 6 ]] echo yes || echo no
no
xiao123xiao123:~/Downloads/shscripts$ [[ 5 6 ]] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [[ 5 6 ]] echo yes || echo no
no
xiao123xiao123:~/Downloads/shscripts$ [[ 5 ! 6 ]] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [[ 5 -ge 6 ]] echo yes || echo no
no
xiao123xiao123:~/Downloads/shscripts$ [[ 5 -le 6 ]] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$ [[ 5 -gt 6 ]] echo yes || echo no
no
xiao123xiao123:~/Downloads/shscripts$ [[ 5 -lt 6 ]] echo yes || echo no
yes
xiao123xiao123:~/Downloads/shscripts$逻辑操作符号测试 -a 与运算 两边都为真结果才为真 || -o 或运算两边有一个为真结果为真 中括号逻辑运算比较
xiao123xiao123:~/Downloads/shscripts$ file1/etc/init.d/rsync
xiao123xiao123:~/Downloads/shscripts$ file2/etc/hostname
xiao123xiao123:~/Downloads/shscripts$ [ -f ${file1} -a -f ${file2} ] echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$ file1/tmp/sqqqq
xiao123xiao123:~/Downloads/shscripts$ [ -f ${file1} -a -f ${file2} ] echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$ [ -f ${file1} -o -f ${file2} ] echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$双中括号运算比较
xiao123xiao123:~/Downloads/shscripts$ str1
xiao123xiao123:~/Downloads/shscripts$ str2yuyu
xiao123xiao123:~/Downloads/shscripts$ [[ -n ${str1} -n ${str2} ]] echo ok || echo no
no
xiao123xiao123:~/Downloads/shscripts$ [[ -n ${str1} || -n ${str2} ]] echo ok || echo no
ok
xiao123xiao123:~/Downloads/shscripts$脚本开发
要求接收用户输入判断它是否等于某个数字。
xiao123xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 3
脚本出错必须输入1和2
xiao123xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 1
1
xiao123xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 2
2
xiao123xiao123:~/Downloads/shscripts$ cat ./test_input.sh
#! /bin/bashread -p Please input a char: var1[ ${var1} -eq 1 ] {echo ${var1}exit 0
}[[ ${var1} 2 ]] {echo ${var1}exit 0
}[ ${var1} ! 2 -a ${var1} ! 1 ] {echo 脚本出错必须输入1和2exit 1
}
xiao123xiao123:~/Downloads/shscripts$要求安装lnmp/lamp脚本开发。
xiao123xiao123:~/Downloads/shscripts$ mkdir test
xiao123xiao123:~/Downloads/shscripts$ echo echo LAMP is installed ./test/lamp.sh
xiao123xiao123:~/Downloads/shscripts$ echo echo LNMP is installed ./test/lnmp.sh
xiao123xiao123:~/Downloads/shscripts$ chmod x ./test/lamp.sh
xiao123xiao123:~/Downloads/shscripts$ chmod x ./test/lnmp.sh
xiao123xiao123:~/Downloads/shscripts$源码
xiao123xiao123:~/Downloads/shscripts$ cat ./test_install.sh
#! /bin/bashpath./test[ ! -d ${path} ] mkdir ${path} -pcat END1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
ENDread numexpr ${num} 1 /dev/null[ $? -ne 0 ] {echo input number must be {1|2|3}exit 1
}[ ${num} -eq 1 ] {echo Strating installing lamp.....waiting...sleep 2[ -x ${path}/lamp.sh ] || {echo The file does not exist or cant be execut.exit 1}${path}/lamp.shexit $?
}[ ${num} -eq 2 ] {echo Strating installing lnmp.....waiting...sleep 2[ -x ${path}/lamp.sh ] || {echo The file does not exist or cant be execut.exit 1}${path}/lnmp.shexit $?
}[ ${num} -eq 3 ] {echo byebyeexit 3
}[[ ! ${num} ~ [1-3] ]] {echo The number input must be {1|2|3}exit 4
}
xiao123xiao123:~/Downloads/shscripts$运行结果
xiao123xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
e
input number must be {1|2|3}
xiao123xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
3
byebye
xiao123xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
1
Strating installing lamp.....waiting...
LAMP is installed
xiao123xiao123:~/Downloads/shscripts$ bash ./test_install.sh1. [install lamp]2. [install lnmp]3. [exit]please input the num you want:
2
Strating installing lnmp.....waiting...
LNMP is installed
xiao123xiao123:~/Downloads/shscripts$