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. #文件存在,并且是普通文件
-L file exists and is a symbolic link. #文件存在,并且符号链接
-p file exists and is a first in, first out (FIFO) special file or named pipe. #文件存在,并且是管道设备或FIFO设备
-S file exists and is a socket. #文件存在,并且是套接字(socket)文件
-s file exists and has a size greater than zero. #文件存在,并且文件大小非零(大于零)

# 判断文件是否存在,并有相关权限的属性

# 常规属性
-r file exists and is readable by the current process. #文件存在,并且本进程可读
-w file exists and is writable by the current process. #文件存在,并且本进程可写
-x file exists and is executable by the current process. #文件存在,并且本进程可执行 # 扩展属性
-u file exists and has its setuid() bit set. #文件存在,并且有setuid属性
-g file exists and has its setgid() bit set. #文件存在,并且有setgid属性
-k file exists and has its sticky bit set. #文件存在,并且有粘滞位
-G file exists and has the same group ID as this process. #文件存在,并且与本进程拥有相同的GID
-O file exists and is owned by the user ID of this process. #文件存在,并且与本进程拥有相同的UID

# 字符长度判断

-z    string length is zero.    #字符串长度为零返回true
-n string length is not zero. #字符串长度非零返回true

# 其他选项(这个地方没太懂,还请各位赐教)

-t    file descriptor number fildes is open and associated with a terminal device.    #文件描述符??是打开的,并且同终端设备有关
-o Named option is set on. #开启了命名选项??

# 两个文件之间的比较

-nt    #newer than,判断 file1 是否比 file2 新,通过比较文件的时间戳实现
-ot #older than,判断 file1 是否比 file2 旧,通过比较文件的时间戳实现
-ef #equal file,判断 file2 与 file2 是否为同一档案,通过判断两个文件指向的的inode是否相同来实现,可用在hard link(硬链接) 的判定上

# 两个整数之间的比较

-eq    #equal  等于
-ne #not equal 不等于
-gt #greater than 大于
-lt #less than 小于
-ge #greater than or equal 大于等于
-le #less than or equal 小于等于

# 多重条件判断

-a    #and 逻辑与,两状况同时成立则返回true
-o #or 逻辑或,两状况任何一个成立则返回true
! #not 逻辑非,状态相反则返回true

常用判断的Shell样例

#!/bin/bash

myPath="/var/log/nginx/"
myFile="/var /log/nginx/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参数 判断一个变量是否是否有值
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

My way on Linux - [Shell基础] - Bash Shell中判断文件、目录是否存在或者判断其是否具有某类属性(权限)的常用方法的更多相关文章

  1. python基础:os模块中关于文件/目录常用的函数使用方法

    Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...

  2. 鸟哥的私房菜:Bash shell(四)-Bash shell的使用环境

    Bash shell(四)-Bash shell的使用环境   是否记得我们登入主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登入的时候,我们还可以给予使用者一些 ...

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

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

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

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

  5. Linux安全基础:shell及一些基础命令

    1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...

  6. Linux零基础之shell基础编程入门

    从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...

  7. linux文件管理之bash shell

    BASH Shell 对文件进行管理 ========================================================创建.复制.删除.移动.查看.编辑.压缩.查找 内 ...

  8. linux系统配置之bash shell的配置(centos)

    linux系统开机启动过程的最后阶段会由init进程根据启动方案(运行级:0-6)启动许多基本的服务程序,为用户提供各种各样的服务.在启动这些服务的最后会启动一个为用户提供操作环境的服务,用户就是通过 ...

  9. 【Linux】-- 认识bash shell

    一.前言 我们知道管理整个计算机硬件的其实是系统的内核,这个内核是需要被保护的,所以我们一般用户就只能通过shell来跟内核通信,以让内核达到我们所想要达到的工作.那么Linux系统有多少shell可 ...

随机推荐

  1. mybatis之特殊查询

    在mybatis查询的过程中,某个字段是经过计算得到的,这时,在设计数据表的时候,就不 必在增加此对应的字段 那么,在查询的时候,页面有需要展示这个字段时,怎么办呢? 举个例子: 在查询微信团商品时, ...

  2. vb的LINQ实现

    vb实现LINQ非常简单的例子: Dim numbers() As Integer = {1, 2, 3, 4, 5, 6, 7} Dim allNumbers = From number In nu ...

  3. eclipse中安装genymotion

    在eclipse中安装genymotion.安装genymotion需要先安装virtualbox.选择Help选项中的install new software 然后点击进去点击ADD,在locati ...

  4. iOS_SN_CoreData(二)

    上一篇是讲怎么进行CoreData的基本操作,这一篇是讲NSFetchedResultsController得到CoreData数据怎么与tableView完美结合,和动画操作的实现. NSFetch ...

  5. java日期操作

    //字符串转日期 public static void dt7() throws ParseException { String str_date="2015---08---08" ...

  6. JS、CSS兼容性问题的几点总结

    javascript和CSS在不同浏览器下的兼容性问题的几点总结: Javascript部分 1. document.form.item 问题问题:代码中存在 document.formName.it ...

  7. swift中的&-备

    参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以引用方式传递,这也是可以实现的,Swift提供 ...

  8. repo init 时gpg: 无法检查签名:找不到公钥

    i found a solution here: http://www.marshut.com/wrrts/repo-release-1-12-4.html Sorry, I realized tod ...

  9. web.xml 3.0头部模板

    <?xml version=”1.0″ encoding=”UTF-8″?><web-appversion=”3.0″xmlns=”http://java.sun.com/xml/n ...

  10. Mysql 新建用户以及授权远程连接操作

    1:以root身份登陆mysql终端 mysql -uroot -pmysql 2:创建wx用户,注意密码要加单引号 mysql> create user wx identified by 'w ...