判断文件是否存在的shell脚本代码

来源:互联网 发布:熊片数据库邀请码 编辑:程序博客网 时间:2024/06/06 10:05

转载地址:http://www.jb51.net/article/34330.htm
-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 这个区别是很大的!
实例代码:

#!/bin/sh# 判断文件是否存在# link:www.jb51.net# date:2013/2/28   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" exit 0fi  # 两个变量判断是否相等if [ "$var1" = "$var2" ]; then echo '$var1 eq $var2'else echo '$var1 not eq $var2'fi 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 撒施复合肥没有充分融化怎么办 绿箩化肥施多了怎么办 辣椒化肥施多了怎么办? 化肥施多了烧苗怎么办 绿植施肥施多了怎么办 盆栽肥料放多了怎么办 三环复合肥怎么办啊 母猪下崽后不吃食怎么办 猪自配料料槽不下怎么办 美甲边缘起翘怎么办 猪粪流到鱼塘里鱼死了怎么办啊 在基本农田建有机肥厂怎么办 有机肥厂的环评怎么办 织玻璃纤维网布环评怎么办 吃了受潮的奶粉怎么办 喝了受潮的奶粉怎么办 刚买的奶粉受潮怎么办 羊不小心吃了化肥怎么办 阿胶粉结成块了怎么办 半桶奶粉受潮了怎么办 眉粉受潮了结块怎么办 刚买的奶粉结块怎么办 袋装白糖成坨了怎么办 一袋子白糖硬了怎么办 粉饼上有一层油怎么办 葡萄后期氮肥施用过多怎么办 没洗的菜吃了怎么办 闻了汽油味头晕怎么办 碰到绿萝的汁液怎么办 吃了带农药水果怎么办 开槽模切一体机模切时开槽怎么办 柔版印刷走纸歪斜怎么办 美团外卖一天8单怎么办 单位显示器丢了怎么办员工赔 纸板板门起泡了怎么办 卖家要我开出质量问题证明怎么办 闲鱼买到的商品不符合描述怎么办 寄出去的东西碎了怎么办 闲鱼快递损坏了怎么办 寄快递东西坏了怎么办 快递邮寄东西坏了怎么办