shell 判断文件、目录是否存在
2. #!/bin/sh
3.
4. myPath="/var/log/httpd/"
5. myFile="/var /log/httpd/access.log"
6.
7. # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
8. if [ ! -x "$myPath"]; then
9. mkdir "$myPath"
10. fi
11.
12. # 这里的-d 参数判断$myPath是否存在
13. if [ ! -d "$myPath"]; then
14. mkdir "$myPath"
15. fi
16.
17. # 这里的-f参数判断$myFile是否存在
18. if [ ! -f "$myFile" ]; then
19. touch "$myFile"
20. fi
21.
22. # 其他参数还有-n,-n是判断一个变量是否是否有值
23. if [ ! -n "$myVar" ]; then
24. echo "$myVar is empty"
25. exit 0
26. fi
27.
28. # 两个变量判断是否相等
29. if [ "$var1" = "$var2" ]; then
30. echo '$var1 eq $var2'
31. else
32. echo '$var1 not eq $var2'
33. fi
-f 和-e的区别
Conditional Logic on Files
-a file exists.
-b file exists and is a block special file.
-c file exists and is a character special file.
-d file exists and is a directory.
-e file exists (just the same as -a).
-f file exists and is a regular file.
-g file exists and has its setgid(2) bit set.
-G file exists and has the same group ID as this process.
-k file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-n string length is not zero.
-o Named option is set on.
-O file exists and is owned by the user ID of this process.
-p file exists and is a first in, first out (FIFO) special file or
named pipe.
-r file exists and is readable by the current process.
-s file exists and has a size greater than zero.
-S file exists and is a socket.
-t file descriptor number fildes is open and associated with a
terminal device.
-u file exists and has its setuid(2) bit set.
-w file exists and is writable by the current process.
-x file exists and is executable by the current process.
-z string length is zero.
是用 -s 还是用 -f 这个区别是很大的!
shell 判断文件、目录是否存在的更多相关文章
- shell判断文件,目录是否存在或者具有权限
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/ ...
- shell判断文件,目录是否存在或者具有权限的代码
核心代码 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的 ...
- shell判断文件,目录是否存在或者具有权限 (转载)
转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...
- linux shell判断文件,目录是否存在或者具有权限
在linux中判断文件,目录是否存在或则具有的权限,根据最近的学习以及网上的资料,进行了以下的总结: #!/bin/sh myPath="/var/log/httpd/" myFi ...
- shell判断文件/目录是否存在
https://www.cnblogs.com/37yan/p/6962563.html caution!!! if should be end with fi caution!!! there sh ...
- 2017.8.23 shell判断文件,目录是否存在或者具有权限
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...
- Shell中判断文件,目录是否存在
一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filena ...
- shell判断文件是否存在
转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...
- Linux shell判断文件和文件夹是否存在
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...
随机推荐
- 【USACO 3.2.5】魔板
[描述] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个 ...
- 较详细的sqlserver数据库备份、恢复(转)
C#实现SQL数据库备份与恢复 有两种方法,都是保存为.bak文件.一种是直接用Sql语句执行,另一种是通过引用SQL Server的SQLDMO组件来实现: .通过执行Sql语句来实现 注意,用Sq ...
- 如何让IIS 8.0支持无后缀图片的访问
进入“MIME类型”模块后,我们点击右侧的“添加”,然后填好文件扩展名和类型值.对于无后缀的图片文件,扩展名只需填写“点”符号即可,类型值根据图片文件实际的扩展名填写.如果是jpeg格式的,那么就填写 ...
- php图片上传
//处理图片 private function imageDeal($param){ $arrType=array('image/jpg','image/bmp','image/png','image ...
- dede 留言板访问的目录
D:\APMServ5.2.6\www\htdocs\xyhy\templets\plus guestbook.rar 文件 里面 DEDE留言簿的插件:
- Python中函数参数传递问题
先上两个例子: http://python.jobbole.com/85231/ a = 1 def fun(a): a = 2 fun(a) print a # 结果为1 fun(a)中的a,可以看 ...
- JQ——选择器
选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的元素 .class $( ...
- openssl 进行证书格式的转换
各类证书由于存储的内容不同(如是否包含公钥/私钥是否加密存储/单一证书或多证书等).采用编码不同(DER/BASE64).标准不同(如PEM/PKCS),所以尽管X.509标准规定了证书内容规范,但证 ...
- jQuery的live绑定事件在mobile safari(iphone / ipad / ipod)上失效的解决方案
jQuery的live绑定为什么会在mobile safari上失效呢?其实可以追溯到jQuery里live的实现方式.live的实现方式实际上是通过事件委托机制来实现的,也就是说是通过诸如冒泡的方式 ...
- 畅谈Spring设计哲学
自己从学习编程开始到现在有一个习惯:一直喜欢把软件开发中的技术和思路放到实际生活中去类比考虑.自己平常也喜欢开一些关于软件哲学的书籍,事实证明这些书籍对自己的学习新技术很有很大的帮助.数学是一切学科的 ...