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. Facebook登录 AndroidStudio

    简介 主页:https://www.facebook.com/ Android开发者支持:https://developers.facebook.com/docs/android/  应用管理:htt ...

  2. 读取xml文件信息

    static void Main(string[] args) { XmlTextReader reader = new XmlTextReader(@"D:\zy\visual studi ...

  3. html表格标签与属性

    标记:  标 记  说 明 <Table> 表格标记 <Tr> 行标记 <Td> 单元格标记  <Th> 表头标记 <Table>标记属性: ...

  4. Ubuntu 添加sudo用户

    第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密 码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到 ...

  5. 禁止chrome中CORS跨域资源共享错误

    在开发中,可以通过命令行命令chrome --allow-file-access-from-files来 禁止CORS错误. 只在紧急情况下使用这个方法,比如你的老板正站在你身后, 并且所有事情都无法 ...

  6. dbcp写连接池 Demo

    1.导包 2.准备配置文件   .properties(注:这里的参数名driverClassName.url.username等是不能改变的) 不能任意命名的原因是[你懂得] 3.Demo publ ...

  7. 详细解析 RxAndroid 的使用方式

    RxAndroid是RxJava的扩展, 优雅地处理异步请求. RxAndroid配合Lambda表达式, 精简处理回调, 使程序更具有可读性. Rx作为Android最优秀的开源库之一, 极大地提高 ...

  8. jquery1.9学习笔记 之选择器(基本元素五)

    多种元素选择器  jQuery("selector1,selector2,selectorN") 例子: <!doctype html> <html lang=' ...

  9. Activiti工作流学习-----基于5.19.0版本(6)

    七. BPMN的简介 读者了解到这里,应付一般的工作流开发已经足够了.此处应该有华丽的分割线,在工作流项目中核心开发人员主要是对工作流业务设计以及实现,而初级开发人员是对业务功能的代码实现.以后将主要 ...

  10. Leakcanary

    一.什么是内存泄漏 垃圾回收器无法回收应被回收的对象比如:在Activity生命周期过程中,旋转屏幕时应新建activity,原activity应被销毁.但如果线程一直在引用此activity,则会出 ...