Linux - 判断文件/目录是否存在/具有权限
来源:互联网 发布:好看的帆布包品牌知乎 编辑:程序博客网 时间:2024/05/29 13:56
一、示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
# 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
if[ ! -x "$myPath"]; then
mkdir"$myPath"
fi
# 这里的-d 参数判断$myPath是否存在
if[ ! -d "$myPath"]; then
mkdir"$myPath"
fi
# 这里的-f参数判断$myFile是否存在
if[ ! -f "$myFile"]; then
touch"$myFile"
fi
# 其他参数还有-n,-n是判断一个变量是否是否有值
if[ ! -n "$myVar"]; then
echo"$myVar is empty"
exit0
fi
# 两个变量判断是否相等
if[ "$var1"= "$var2"]; then
echo'$var1 eq $var2'
else
echo'$var1 not eq $var2'
fi
二、常用判定符
-c :文件存在并且是一个字符特殊文件.
-d :文件存在并且是一个目录
-e :文件存在(如 -a 一样)
-f :文件存在并且是一个普通文件
-L :文件存在并且是符号链接.
-n :字符串长度不为零.
-s :文件已存在,并且大小大于零.
-w :文件存在并且是由当前进程可写.
-x :文件存在并且是可执行的过程.
-z :字符串的长度是零
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
# 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
if[ ! -x "$myPath"]; then
mkdir"$myPath"
fi
# 这里的-d 参数判断$myPath是否存在
if[ ! -d "$myPath"]; then
mkdir"$myPath"
fi
# 这里的-f参数判断$myFile是否存在
if[ ! -f "$myFile"]; then
touch"$myFile"
fi
# 其他参数还有-n,-n是判断一个变量是否是否有值
if[ ! -n "$myVar"]; then
echo"$myVar is empty"
exit0
fi
# 两个变量判断是否相等
if[ "$var1"= "$var2"]; then
echo'$var1 eq $var2'
else
echo'$var1 not eq $var2'
fi
二、常用判定符
-a :文件存在
-b :文件存在并且是一块特殊文件-c :文件存在并且是一个字符特殊文件.
-d :文件存在并且是一个目录
-e :文件存在(如 -a 一样)
-f :文件存在并且是一个普通文件
-L :文件存在并且是符号链接.
-n :字符串长度不为零.
-s :文件已存在,并且大小大于零.
-w :文件存在并且是由当前进程可写.
-x :文件存在并且是可执行的过程.
-z :字符串的长度是零
0 0
- Linux - 判断文件/目录是否存在/具有权限
- Linux - 判断文件/目录是否存在/具有权限
- linux shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- [zz] shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- shell判断文件,目录是否存在或者具有权限
- Oracle 复制任意表一行的SQL语句(测试Ok)
- 点绛唇-聊以三年
- centos 源码安装mysql 5.6.14
- hdu 3938
- CX控件记录
- Linux - 判断文件/目录是否存在/具有权限
- jQuery插入,复制、替换和删除节点
- Ajax使用AntiForgeryToken,记录一下
- LeetCode OJ - Valid Number
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
- LeetCode 29 Best Time to Buy and Sell Stock III
- 沁园春
- [编程好习惯] 精确包含头文件(转)
- WebLogic Enterprise JavaBean 编程