【shell基础】条件测试

例1 判断目录是否存在

#!/usr/bin/bash
back_dir=/var/mysql_back
if ! test -d $back_dir;then
    mkdir -p $back_dir
fi

例2 安装软件

#!/usr/bin/bash
if [ $UID -ne 0 ];then
    echo "no permission"
    exit
fi
yum -y install httpd

文件测试(操作符 文件或目录)

test -d /home
echo $?
输出:0
test -d /home1111
echo $?
输出:1
[ -e dir|file]  判断目录或文件是否存在
[ -d dir ] 判断是否存在 而且是目录
[ -f file ] 判断是否存在 而且是文件
[ -r file ] 当前用户对该文件是否有读权限
[ -x file ]
[ -w file ]
[ -L file ] 判断是否为链接

猜你喜欢

转载自www.cnblogs.com/zhangshengxiang/p/11536616.html