一。read命令
-p:输出提示消息
-t :设置超时时间
二。条件测试
得出真or假
test命令:真:0
1.-e:文件是否存在
linux@ubuntu:~/shell$ test -e hihi.sh
linux@ubuntu:~/shell$ echo $?
1
举例说明
2.test可以有逻辑运算
linux@ubuntu:~/shell$ test -e gg.txt && echo "hello" || touch "gg.txt"
hello
3.-f :判断文件是否为普通文件
-d :判断是否为目录
-z :判断字符串为空
-n :判断字符串不为空
linux@ubuntu:~/shell$ test -n " " && echo "yes"||echo "no"
yes
linux@ubuntu:~/shell$ test -z " " && echo "yes"||echo "no"
no
4.中括号[]--条件测试
linux@ubuntu:~/shell$ ls
gg.txt nginx sub_str
linux@ubuntu:~/shell$ name=gg.txt
linux@ubuntu:~/shell$ [ -f "${name}" ]&& echo ok ||echo no
ok
5.字符串比较
= :是不是相等
!= :是不等于的
!:反义
linux@ubuntu:~/shell$ [ "a" = "a" ] && echo ok || echo no
ok
linux@ubuntu:~/shell$ [ ! -f "gtgt.txt" ]&& echo ok ||echo no
ok
linux@ubuntu:~/shell$ ls
gg.txt hhh.txt nginx sub_str
6.数值比较
必须加转义符号\
linux@ubuntu:~/shell$ [ 2 \> 1 ] && echo ok || echo no
ok
linux@ubuntu:~/shell$ [ 2 -gt 1 ] && echo ok || echo no
ok
7.逻辑运算
linux@ubuntu:~/shell$ [ -f "file1" -a -f "feil2" ] && echo ok || echo no
no
-a :&&
-o :||
! :!取反
脚本举例:
#!/bin/bash
read -p "pls input char : " var1
[ "$var1" -eq "1" ] &&
{
echo ${var1}
exit 0
}
[ "$var1" != "1" -a "$var1" != "2" ] &&
{
echo "error"
exit 1
}