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

 更新时间:2013年02月28日 10:41:14   投稿:mdxy-dxy  
判断文件是否存在的shell脚本,有详细的注释,很不错,有需要的朋友不妨参考下

实现代码一、

#!/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判断文件夹是否存在

#如果文件夹不存在,创建文件夹
if [ ! -d "/myfolder" ]; then
 mkdir /myfolder
fi

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

folder="/var/www/"
file="/var/www/log"

# -x 参数判断 $folder 是否存在并且是否具有可执行权限
if [ ! -x "$folder"]; then
 mkdir "$folder"
fi

# -d 参数判断 $folder 是否存在
if [ ! -d "$folder"]; then
 mkdir "$folder"
fi

# -f 参数判断 $file 是否存在
if [ ! -f "$file" ]; then
 touch "$file"
fi

# -n 判断一个变量是否有值
if [ ! -n "$var" ]; then
 echo "$var is empty"
 exit 0
fi

# 判断两个变量是否相等
if [ "$var1" = "$var2" ]; then
 echo '$var1 eq $var2'
else
 echo '$var1 not eq $var2'
fi

-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 这个区别是很大的!

相关文章

  • Shell脚本echo指令使用小技巧

    Shell脚本echo指令使用小技巧

    这篇文章主要介绍了Shell脚本echo指令使用小技巧,包括使用echo指令输出换行、输出不换行、输出变量等技巧,需要的朋友可以参考下
    2014-07-07
  • shell脚本之sed详细用法详解

    shell脚本之sed详细用法详解

    Sed是一个非交互性文本流编辑器,它编辑文件或标准输入导出的文本拷贝,vi中的正则表达式命令在sed中大多可以通用,下面这篇文章主要给大家介绍了关于shell脚本之sed详细用法的相关资料,需要的朋友可以参考下
    2022-07-07
  • shell实现俄罗斯方块脚本

    shell实现俄罗斯方块脚本

    这篇文章主要为大家详细介绍了shell实现俄罗斯方块的脚本,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • linux shell中“.” 和 “./”执行的区别详解

    linux shell中“.” 和 “./”执行的区别详解

    这篇文章主要介绍了linux shell中“.” 和 “./”执行的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 防止ARP攻击的shell代码

    防止ARP攻击的shell代码

    防止ARP攻击,使用命令route、grep、ifconfig等,需要的朋友可以参考下
    2013-02-02
  • Linux中的grep -v、-e、-E用法小结

    Linux中的grep -v、-e、-E用法小结

    grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来,这篇文章主要介绍了Linux之grep -v、-e、-E用法总结,需要的朋友可以参考下
    2022-11-11
  • awk中让人郁闷的system()函数

    awk中让人郁闷的system()函数

    system()的结果是直接返回给shell显示了,然后再由awk继续执行后面的程序,这种情况下,if()里留下的其实是system()的执行状态【即0或1】”0”~/^[2 3]/,当然就一直执行else了
    2013-03-03
  • 深入理解Linux中的grep命令

    深入理解Linux中的grep命令

    大家都知道grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。这篇文章给大家详细的介绍了Linux中的grep命令,相信对大家的学习和理解很有帮助,有需要的朋友们可以参考借鉴,感兴趣下面来一起学习学习吧。
    2016-11-11
  • Linux du命令查看文件夹大小并按降序排列

    Linux du命令查看文件夹大小并按降序排列

    这篇文章主要介绍了Linux du命令查看文件夹大小并按降序排列,需要的朋友可以参考下
    2015-11-11
  • 区分shell中的 反引号、$()和${}

    区分shell中的 反引号、$()和${}

    这篇文章主要介绍了区分shell中的 反引号、$()和${},帮助大家更好的理解和学习shell,感兴趣的朋友可以了解下
    2020-08-08

最新评论