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. ValidateBox( 验证框) 组件

    本节课重点了解 EasyUI 中 ValidateBox(验证框)组件的使用方法,这个组件依赖于Tooltip(提示框)组件. 一. 加载方式//class 加载方式<input id=&quo ...

  2. ViewPager欢迎页

    布局  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  3. C# 计算日期时间的间隔天数

    DateTime oldDate = ,,); DateTime newDate = DateTime.Now; // Difference in days, hours, and minutes. ...

  4. scala学习笔记——特质

    一个类扩展自一个或多个特质,以便使用这些特质提供的服务.特质可能会要求使用它的类支持某个特定的特性.不过和java不同,Scala特质可以给出这些特性的缺省实现. 特质的特性: 类可以实现任意数量的特 ...

  5. scala学习笔记——类和对象

    基础语法关于Scala程序,这是非常要注意以下几点. 区分大小写 - Scala是大小写敏感的,这意味着标识Hello 和 hello在Scala中会有不同的含义. 类名 - 对于所有的类名的第一个字 ...

  6. SwfUpload及imgareaselect使用方法

    1.导入文件 Swfupload相关文件 imgareaselect截取插件相关文件 2.前端html代码 添加一个截取图片的按钮,其他为swf所需的html. <body> <di ...

  7. git 笔记记录

    分布式版本控制系统Git 是一套内容寻址文件系统,从核心上来看不过是简单地存储键值对.一: git 本地clone 一个仓库    1. 直接clone一个仓库:        $: git clon ...

  8. 验证码 Demo

    //设置响应头 response.setCharacterEncoding("image/jpeg"); int width=160; int height=40; Buffere ...

  9. kafka安装及Kafka-PHP扩展的使用

    话说用了就要有点产出,要不然过段时间又忘了,所以在这里就记录一下试用Kafka的安装过程和php扩展的试用. 实话说,如果用于队列的话,跟PHP比较配的,还是Redis.用的顺手,呵呵,只是Redis ...

  10. Python读取Yaml文件

    近期看到好多使用Yaml文件做为配置文件或者数据文件的工程,随即也研究了下,发现Yaml有几个优点:可读性好.和脚本语言的交互性好(确实非常好).使用实现语言的数据类型.有一个一致的数据模型.易于实现 ...