shell判断文件,目录是否存在或者具有权限 

#!/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" 

 exit 0 

 fi 

 

 # 两个变量判断是否相等 

 if [ "$var1" = "$var2" ]; then 

 echo '$var1 eq $var2' 

 else 

 echo '$var1 not eq $var2' 

 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判断文件,目录是否存在或者具有权限的更多相关文章

  1. shell判断文件,目录是否存在或者具有权限的代码

    核心代码 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的 ...

  2. shell判断文件,目录是否存在或者具有权限 (转载)

    转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...

  3. linux shell判断文件,目录是否存在或者具有权限

    在linux中判断文件,目录是否存在或则具有的权限,根据最近的学习以及网上的资料,进行了以下的总结: #!/bin/sh myPath="/var/log/httpd/" myFi ...

  4. 2017.8.23 shell判断文件,目录是否存在或者具有权限

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  5. shell判断文件/目录是否存在

    https://www.cnblogs.com/37yan/p/6962563.html caution!!! if should be end with fi caution!!! there sh ...

  6. shell 判断文件、目录是否存在

    shell判断文件是否存在   1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. m ...

  7. Shell中判断文件,目录是否存在

    一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filena ...

  8. shell判断文件是否存在

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  9. Linux shell判断文件和文件夹是否存在

    shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...

随机推荐

  1. 数据解析框架之Gson

    GSON是由Google提供的,用于JAVA对象与JSON字符串之间互相转换的一个解决方案.用处很多,比如在前后台通讯中,前台先将java对象转化成JSON,然后将JSON传输给后台交由后台处理——相 ...

  2. NavMenu 导航菜单

    顶栏 适用广泛的基础用法. 导航菜单默认为垂直模式,通过mode属性可以使导航菜单变更为水平模式.另外,在菜单中通过submenu组件可以生成二级菜单.Menu 还提供了background-colo ...

  3. 热门搜索词获取java版

    日常生活中经常会有这样的需求,就是网站上.app上需要展示近期热搜的一些词汇,其实常用的做法当然是自己写爬虫去爬,不过这种办法是在太麻烦了,为了节省时间,可以调用一些接口来实现,比如常用的聚合数据,小 ...

  4. SQLserver本地数据库开发

    远程端数据库中生成脚本 注意 远程端的数据库 是中文版的还是英文版的,一般我们装的是英文版的, 如果远程端数据库是中文版的,那么我们本地的是英文版,在生成的脚本那需要修改,同时去除相应的路劲代码. 修 ...

  5. react 添加代理 proxy

    react package.json中proxy的配置如下 "proxy": { "/api/rjwl": { "target": &quo ...

  6. Python学习之认知(二)

    第二章(二) 2.1.5 变量 ​ 变量:将运算的中间结果暂存到内存,以便后续程序调⽤. 变量的命名规则: ​ 1.变量由字母, 数字,下划线搭配组合⽽成 ​ 2.不可以⽤数字开头,更不能是全数字 ​ ...

  7. CornerNet-Lite算法笔记

    论文名称:CornerNet-Lite: Efficient Keypoint Based Object Detection 论文链接:https://arxiv.org/abs/1904.08900 ...

  8. 一篇文章搞懂python2、3编码

    说在前边: 编码问题一直困扰着每一个程序员的编程之路,如果不将它彻底搞清楚,那么你的的这条路一定会走的格外艰辛,尤其是针对使用python的程序员来说,这一问题更加显著, 因为python有两个版本, ...

  9. zabbix监控ssl证书过期时间

    获取证书过期时间脚本: /etc/zabbix/scripts/check-cert-expire.sh: #!/bin/bash host=$ port=$ end_date=`/usr/bin/o ...

  10. Quartz持久化到mongodb

    springboot中集成quzrtz ,持久到mongodb 1.pom引用 <?xml version="1.0" encoding="UTF-8"? ...