`

(转) 判断文件是否存在的shell脚本代码

阅读更多

转载地址:http://www.jb51.net/article/34330.htm 
-f 和-e的区别 
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. 
-g file exists and has its setgid(2) bit set. 
-G file exists and has the same group ID as this process. 
-k file exists and has its sticky bit set. 
-L file exists and is a symbolic link. 
-n string length is not zero. 
-o Named option is set on. 
-O file exists and is owned by the user ID of this process. 
-p file exists and is a first in, first out (FIFO) special file or named pipe. 
-r file exists and is readable by the current process. 
-s file exists and has a size greater than zero. 
-S file exists and is a socket. 
-t file descriptor number fildes is open and associated with a terminal device. 
-u file exists and has its setuid(2) bit set. 
-w file exists and is writable by the current process. 
-x file exists and is executable by the current process. 
-z string length is zero. 
是用 -s 还是用 -f 这个区别是很大的! 
实例代码:

#!/bin/sh
# 判断文件是否存在
# link:www.jb51.net
# date:2013/2/28  
 myPath="/var/log/httpd/"
myFile="/var /log/httpd/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,-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 

 

分享到:
评论

相关推荐

    判断文件是否存在的shell脚本代码

    判断文件是否存在的shell脚本,有详细的注释,很不错,有需要的朋友不妨参考下

    shell脚本实现本地文件与服务器文件同步

    主要介绍了本地文件与服务器文件同步shell脚本的方法,然后在文章给大家补充介绍了shell脚本备份本地服务器的文件到远程服务器的实例代码,需要的朋友可以参考下

    批量转换目录下文件编码的shell脚本代码

    一例批量转换目录下文件编码的shell脚本代码。 需求描述:由于从window转linux过来,很多原来win下的gbk文件需要转换成utf8。 以下脚本仅判断非utf8文件转换成utf8文件,并且默认非utf8文件为gbk,如果文件类型不...

    在指定目录查找指定后缀文件的shell脚本代码

    用shell脚本实现的在指定目录查找指定后缀的文件,需要的朋友可以参考下

    精通UNIX Shell脚本编程(附源代码)

    Randal K.Michael是一位在可口可乐公司任职的UNIX系统管理员,他编写了许多shell脚本来处理 UNIX下复杂的系统监视和事件通知问题。他具有23年的工作经验:同时担任UNIX系统管理员达10年 之久,熟悉Solaris、Linux、...

    一个监控LINUX目录和文件变化的Shell脚本分享

    最近看到群里有人聊到他们的服务器最近被挂马,然后想利用一个脚本能够实时...注:原理实际上利用的是du -sb输出值来判断文件的变化,再利用diff进行比对。 1.在执行脚本前要保存原始的状态: # vi initial.sh 代码如

    Linux云计算之Shell脚本.zip

    11、数据增量备份脚本代码.mp4 12、DNS服务.mp4 13、重定向.mp4 14、脚本代码.mp4 15、管理系统用户命令.mp4 16、Shell流程控制.mp4 17、Shell条件判断.mp4 18、脚本代码.mp4 字符串处理-01、字符串处理.mp4 数值...

    Shell脚本实现检测文件是否被修改过代码分享

    #!/bin/bash funmd5_1() { find /root/passwd -type f | xargs md5sum > /tmp/funmd5_1.log } funmd5_2() { find /root/passwd -type f | xargs md5sum > /tmp/funmd5_2.log } if [ ! -f /tmp/funmd5_1.log ];...

    Shell脚本定期清空大于1G的日志文件

    一个关于如何在指定文件大于1GB后,自动删除的问题。 批处理代码如下: 代码如下: #!/bin/bash # 当/var/log/syslog大于1GB时 # 自动将其备份,并清空 # 注意这里awk的使用 if ! [ -f /var/log/syslog ] ...

    shell脚本 自动创建用户详解及实例代码

    shell脚本 自动创建用户详解 需求:判断用户zhangsan是否存在,不存在就创建并设置密码为123456 1、vi createuser.sh 2、写入: USER_COUNT=`cat /etc/passwd | grep '^zhangsan:' -c` USER_NAME='zhangsan' if ...

    查找目录下同名但不同后缀名文件的shell脚本代码

    个脚本可以实现指定目录下同名但不同后缀名的查找,可以拓展为删除指定的文件的脚本,觉得很实用,分享一下

    shell脚本返回值及其使用场景的实现

    在一些应用中(比如Jenkins),嵌入了shell脚本,系统通过shell脚本的返回值来判断执行结果,如果返回值非0,则发生了执行错误,需要中止执行,这在使用单个命令时没有问题。然而,在shell (A)脚本又调用了shell...

    Shell中调用、引用、包含另一个脚本文件的三种方法

    脚本 first (测试示例1) first#!/bin/bashecho ‘your are in first file’ 方法一:使用source #!/bin/bashecho ‘your are in second file’source first ... 您可能感兴趣的文章:判断文件是否存在的shell脚本代码Sh

    shell脚本实现实时检测文件变更

    主要介绍了shell脚本实现实时检测文件变更,本文直接给出实现代码和使用方法,以及svn下的实现代码,需要的朋友可以参考下

    Linux shell实现压缩多个文件代码实例

    Linux环境下写一个脚本 ... 您可能感兴趣的文章:linux shell 根据进程名获取pid的实现方法linux shell 中判断文件、目录是否存在的方法linux shell中“.” 和 “./”执行的区别详解Linux Shell Map的用法详解Lin

    Shell脚本中判断输入参数个数的方法

    $#代表了命令行的参数数量,可以看...shell 编程中使用到得if语句内判断参数 –b 当file存在并且是块文件时返回真 -c 当file存在并且是字符文件时返回真 -d 当pathname存在并且是一个目录时返回真 -e 当pathname指定

    shell判断文件,目录是否存在或者具有权限的代码

    #这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x $myPath]; then mkdir $myPath fi #这里的-d 参数判断$myPath是否存在 if [ ! -d $myPath]; then mkdir $myPath fi #这里的-f参数判断$myFile...

Global site tag (gtag.js) - Google Analytics