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. pandas中的reset_index()

    数据清洗时,会将带空值的行删除,此时DataFrame或Series类型的数据不再是连续的索引,可以使用reset_index()重置索引. import pandas as pd import nu ...

  2. spring_quartz的实现

    一.在spring配置文件中引用对应的定时任务配置文件 二.定义定时任务的业务代码 三.配置定时任务配置文件spring-quartz.xml <?xml version="1.0&q ...

  3. LoadRunner 技巧之协议分析

    LoadRunner 技巧之协议分析 在做性能测试的时候,协议分析是困扰初学者的难题,选择错误的协议会导致Virtual User Generator 录制不到脚本:或录制的脚本不完整,有些应用可能需 ...

  4. Java split(".") 和 split("\\.")

    Java split(".") 和 split("\\.") 问题描述 使用 . 分解 IP 的各个段,并打印,如:192.168.10.123,分解为 192 ...

  5. docker上传私有仓库报错

    docker 1.17 版本搭建私有仓库,上传镜像报错:server gave HTTP response to HTTPS client” 系统环境:centos7docker版本:1.17(注意版 ...

  6. Window Relationships and Frames

    If a page contains frames, each frame has its own window object and is stored in the frames collecti ...

  7. CentOS7 执行yum 命令出错

    http://blog.csdn.net/weicuidi/article/details/52935100 https://www.cnblogs.com/pqchao/p/6428947.html

  8. Vue组件中的data属性

    Vue中的data属性专门用来以对象方式存放数据,它有两种用法. var vm=new Vue({ data:{a:1,b:2,}, }) var vm=new Vue({ data(){return ...

  9. 【Linux开发】linux设备驱动归纳总结(三):7.异步通知fasync

    linux设备驱动归纳总结(三):7.异步通知fasync xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  10. CVE-2018-2628

    哈哈哈,终于等到我复现一个CVE漏洞了. 漏洞描述 Oracle Fusion Middleware(Oracle融合中间件)是美国甲骨文(Oracle)公司的一套面向企业和云环境的业务创新平台.该平 ...