经常使用条件测试有 test 命令,[] [[]]等。mongodb
##-f 检查是否为文件 [root@promote ~]# ls anaconda-ks.cfg test.txt [root@promote ~]# test -f test.txt && (echo 0 || echo 1) 0 [root@promote ~]# test -f test1.txt && (echo 0 || echo 1) [root@promote ~]# test -f test.txt && echo 0 || echo 1 0 [root@promote ~]# test -f test.txt1 && echo 0 || echo 1 1 [root@promote ~]#
详细信息请看。docker
文件表达式 -e:文件存在 -f:表示这个文件是个通常文件(regular file)(不是目录或者设备文件) 文件表达式 -e filename 若是 filename文件存在,则为真 -d filename 若是 filename为目录,则为真 -f filename 若是 filename为是般文件(regular file)(不是目录或者设备文件),则为真 -L filename 若是 filename为符号连接,则为真 -r filename 若是 filename可读,则为真 -w filename 若是 filename可写,则为真 -x filename 若是 filename可执行,则为真 -s filename 若是文件长度不为0,则为真 -h filename 若是文件是软连接,则为真 filename1 -nt filename2 若是 filename1比 filename2新,则为真。 filename1 -ot filename2 若是 filename1比 filename2旧,则为真。 整数变量表达式 比较符号 (( ))中[[ ]]使用符号 说明 -eq 等于 ==或= equal -ne 不等于 != not equal -gt 大于 > greater than -ge 大于等于 >= greater equal -lt 小于 < less than -le 小于等于 <= less equal
条件判断中test 和 [ ]等价。(( ))支持简单运算,[ ] 与[[]]不支持,[[ ]]支持&&和||。比较运算符先后推荐添加空格。bash
详细用法能够查看帮助。less
#没有全面列举 [root@promote ~]# man test [root@promote ~]# man bash [root@promote ~]# info bash [root@promote ~]# man bash #部分帮助文件以下 -a FILE True if file exists. -b FILE True if file is block special. -c FILE True if file is character special. -d FILE True if file is a directory. -e FILE True if file exists. -f FILE True if file exists and is a regular file. -g FILE True if file is set-group-id. -h FILE True if file is a symbolic link. -L FILE True if file is a symbolic link. -k FILE True if file has its `sticky' bit set. -p FILE True if file is a named pipe. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -S FILE True if file is a socket. -t FD True if FD is opened on a terminal. -u FILE True if the file is set-user-id. -w FILE True if the file is writable by you. -x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. -N FILE True if the file has been modified since it was last read.
注意对比如下代码输出结果。socket
[root@promote ~]# [ -f /etc/passwd -a /etc/services ] && echo 0 || echo 1 0 [root@promote ~]# [[ -f /etc/passwd -a /etc/services ]] && echo 0 || echo 1 #错误使用-a [root@promote ~]# [[ -f /etc/passwd && /etc/services ]] && echo 0 || echo 1 0 [root@promote ~]# [ -f /etc/passwd && /etc/services ] && echo 0 || echo 1 #错误使用&&
本节内容经过一段简单代码结束。测试
#只能输入数字一、二、3 #save a .sh file #!/bin/bash testdir=/root/testdir [! -d testdir ] && mkdir -p /root/testdir cat << END 1.[install lamp] 2.[install lnmp] 3.[exit] please input 1 or 2 or 3 : END read input expr $input + 0 &>/dev/null #Not necessarily 0, /dev//null means not output to standard output interface [ $? -ne 0 ] && { echo "please input {1|2|3}" exit 1 } [ $? -ne 1 ] && { echo "Start install lamp ..." exit 1 } [ $? -ne 2 ] && { echo "Start install lnmp ..." exit 1 } [root@promote ~]# ls anaconda-ks.cfg get-docker.sh install_mongodb.sh test testdir.sh test.txt users [root@promote ~]# #简单目录是否存在判断 #查看文件内容 [root@promote ~]# cat testmkdir.sh #!/bin/bash if [ ! -d /root/var/testdir ] ;then mkdir -p /root/var/testdir fi #执行脚本 [root@promote ~]# bash testmkdir.sh [root@promote ~]# ls /root/var/testdir | grep t testdir [root@promote ~]#