My way on Linux - [Shell基础] - Bash Shell中判断文件、目录是否存在或者判断其是否具有某类属性(权限)的常用方法
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中判断文件、目录是否存在或者判断其是否具有某类属性(权限)的常用方法的更多相关文章
- python基础:os模块中关于文件/目录常用的函数使用方法
Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...
- 鸟哥的私房菜:Bash shell(四)-Bash shell的使用环境
Bash shell(四)-Bash shell的使用环境 是否记得我们登入主机的时候,屏幕上头会有一些说明文字,告知我们的 Linux 版本啊什么的, 还有,登入的时候,我们还可以给予使用者一些 ...
- Shell中判断文件,目录是否存在
一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filena ...
- shell判断文件,目录是否存在或者具有权限
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/ ...
- Linux安全基础:shell及一些基础命令
1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...
- Linux零基础之shell基础编程入门
从程序员的角度来看, Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操 ...
- linux文件管理之bash shell
BASH Shell 对文件进行管理 ========================================================创建.复制.删除.移动.查看.编辑.压缩.查找 内 ...
- linux系统配置之bash shell的配置(centos)
linux系统开机启动过程的最后阶段会由init进程根据启动方案(运行级:0-6)启动许多基本的服务程序,为用户提供各种各样的服务.在启动这些服务的最后会启动一个为用户提供操作环境的服务,用户就是通过 ...
- 【Linux】-- 认识bash shell
一.前言 我们知道管理整个计算机硬件的其实是系统的内核,这个内核是需要被保护的,所以我们一般用户就只能通过shell来跟内核通信,以让内核达到我们所想要达到的工作.那么Linux系统有多少shell可 ...
随机推荐
- CentOS yum Fatal Error 处理一例
环境说明 [root@thatsit ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@thatsit ~]# uname - ...
- Sass运算
加法在 CSS 中能做运算的,到目前为止仅有 calc() 函数可行.在 Sass 中,运算只是其基本特性之一.在 Sass 中可以做各种数学计算.加法运算是 Sass 中运算中的一种,在变量或属性中 ...
- javascript 实现jsonp
jsonp原理其实也简单,虽然ajax不能跨域,但是通过src这个属性我们可以实现跨域,其实和我们引入第三方jquery调用它的方法一样的. html: <!DOCTYPE html> & ...
- 在div+css中用到的js代码注意return
今天做了一个项目,美工做好后放在了form中(没有加runat=server),由于用到了服务器控件,所以这里要加,否则报错,关键一段div代码是: <form id="form_re ...
- struct可以拥有class般的构造函数
struct A { int a, b; A(int x, int y) :a(x), b(y){} }; int main() { A a(1, 2); cout << a.a < ...
- C++编译指令#pragma pack的配对使用
#pragma pack可以用来指定C++数据结构的成员变量的内存对齐数值(可选值为1,2,4,8,16). 本文主要是强调在你的头文件中使用pack指令要配对使用,以避免意外影响项目中其他源文件的结 ...
- Blocks(POJ 3734 矩阵快速幂)
Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...
- iOS开发之Runtime函数
1.可以通过NSObject的一些方法获取运行时信息或动态执行一些消息: 1./*Returns a Boolean value that indicates whether the receivin ...
- SQL Fetch size
JDBC performance tuning with optimal fetch size February 1, 2009 31 Comments Tuning performance usin ...
- 这样就算会了PHP么?-7
循环之类的例子 <script language="javascript"> function calculate(a, b) { return a * b; } do ...