文件、目录、权限判断语法
-e "$path" #判断文件或目录是否存在 exits
-f "$path" #判断文件是否存在 file
-d "$path" #判断目录是否存在 directory
-w "$path" #判断是否可写 write
-r "$path" #判断是否可读 read
-x "$path" #判断是否可执行 execute
文件判断测试
[ -e "/etc/passwd" ]
echo $?
[ -e "/etc/" ]
echo $?
[ -e "/tmp/nononono" ]
echo $?
[ -f "/tmp/" ]
echo $?
[ -x "/etc/passwd" ]
echo $?
[ -x "/bin/bash" ]
echo $?
举例如下
filename="/etc/passwd"
if [ -f "$filename" ];then
echo "$filename is a file"
else
echo "$filename is not a file"
fi
nginx安装脚本改善,判断是否已经安装nginx
if [ -d "/usr/local/nginx" ];then
echo "nginx is installed. exit"
exit 1
fi
nginxdownloadurl="http://nginx.org/download/nginx-1.12.2.tar.gz"
yum -y install wget gcc pcre pcre-devel zlib zlib-devel
cd /usr/local/src/
wget "$nginxdownloadurl"
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx
make
make install