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. NDK的环境配置

    http://www.androiddevtools.cn/ 下载NDK, 最新版本. 解压压缩包,如解压后文件夹名为如android-ndk-r13,放在指定的位置 配置环境变量: 系统环境path ...

  2. Python之异常处理合集

    PermissionError: [Errno 13] Permission denied open(filePath)中的filePath是一目录路径,而非目录路径 先前打开的file文件对象未被关 ...

  3. Python之数据库操纵工具

    前言 基于原生模块:pymysql 推荐教程 Python 数据库处理的类库 pymysql [python原生模块] SQLAchemy [ORM框架] Python3 MySQL 数据库连接 - ...

  4. POJO是什么,javabean是什么,以及POJO与javabean的区别

    POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称.使用POJO名称是为了避免和EJB混淆起来, 而 ...

  5. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

  6. 转:async异步、thread多线程

    很全面的知识,转来留着 1:https://www.cnblogs.com/xibei/p/11826498.html 2:https://www.cnblogs.com/xibei/p/118742 ...

  7. 7-1 shell编程基础之二

    shell编程基础之二 算数运算 bash中的算术运算:help let +, -, *, /, %取模(取余), **(乘方),乘法符号有些场景中需要转义 实现算术运算: (1) let var=算 ...

  8. cisco三层交换为vlan配置dhcp

        dhcp(config)#vlan 2 dhcp(config-vlan)#name sales dhcp(config-vlan)#vlan 3 dhcp(config-vlan)#name ...

  9. 【Python开发】python读写文件,和设置文件的字符编码比如utf-8

    一. python打开文件代码如下: f = open("d:\test.txt", "w") 说明: 第一个参数是文件名称,包括路径: 第二个参数是打开的模式 ...

  10. OpenTSDB在HBase中的底层数据结构设计

    0.时序数据库 时间序列(Time Series):是一组按照时间发生先后顺序进行排列的数据点序列,通常一组时间序列的时间间隔为一恒定值(如1秒,5分钟,1小时等). 时间序列数据可被简称为时序数据. ...