shell脚本判断文件是否存在
在进行linux系统相关应用程序开发时,少不了要书写一些shell脚本,有时候要用到判断文件或者目录是否存在的脚本,本文笔者做一下笔记,已备后查。
shell判断文件是否存在的脚本如下:
其实shell对于文件册测试有好几种选项开关
现在例举如下:
表达式测试含义-a filepathfile exists. all files type-b filepathfile exists and is a block special file.-c filepathfile exists and is a character special file.-d filepathfile exists and is a directory.-e filepathfile exists (等同于 -a).-f filepathfile exists and is a regular file.-g filepathfile exists and has its setgid(2) bit set.-G filepathfile exists and has the same group ID as this process.-k filepathfile exists and has its sticky bit set.-L filepathfile exists and is a symbolic link.-n filepathstring length is not zero.-o filepathNamed option is set on.-O filepathfile exists and is owned by the user ID of this process.-p filepathfile exists and is a first in, first out (FIFO) special file ornamed pipe.-r filepathfile exists and is readable by the current process.-s filepathfile exists and has a size greater than zero.-S filepathfile exists and is a socket.-t filepathfile descriptor number fildes is open and associated with aterminal device.-u filepathfile exists and has its setuid(2) bit set.-w filepathfile exists and is writable by the current process.-x filepathfile exists and is executable by the current process.
shell脚本之文件是否存在、权限校验
判断目录是否存在
判断文件是否存在
判断目录/文件是否存在
判断文件权限
判断文件是否属于当前用户
判断文件是否与当前用户相同用户组
比较文件之间是否为新建
复合条件判断文件
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。