1. shell判断文件,目录是否存在或者具有权限
2. #!/bin/sh
3.
4. myPath="/var/log/httpd/"
5. myFile="/var /log/httpd/access.log"
6.
7. # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
8. if [ ! -x "$myPath"]; then
9. mkdir "$myPath"
10. fi
11.
12. # 这里的-d 参数判断$myPath是否存在
13. if [ ! -d "$myPath"]; then
14. mkdir "$myPath"
15. fi
16.
17. # 这里的-f参数判断$myFile是否存在
18. if [ ! -f "$myFile" ]; then
19. touch "$myFile"
20. fi
21.
22. # 其他参数还有-n,-n是判断一个变量是否是否有值
23. if [ ! -n "$myVar" ]; then
24. echo "$myVar is empty"
25. exit 0
26. fi
27.
28. # 两个变量判断是否相等
29. if [ "$var1" = "$var2" ]; then
30. echo '$var1 eq $var2'
31. else
32. echo '$var1 not eq $var2'
33. 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自动化脚本,自动判断文件夹(目录)是否存在,如不存在自动创建,可执行,下载可测试
第二步,检测文件或目录是否存在: hdfs dfs -test -e 文件或目录名 第三步,查看检测结果: echo $? 若输出结果为0,则说明文件或目录存在,若为1,则说明文件或目录不存在。 二、Java代码实现 import org.apache...
主要介绍了linux shell 中判断文件、目录是否存在的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
前言 大家在工作的时候可能经常会遇到这样的需求,在类Unix系统的Bash环境下,怎样检查...下面的命令,则使用Shell的条件表达式,判断/etc/hosts文件是否存在: [ -f /etc/hosts ] && echo Found || echo Not found
# 判断文件是否存在 # link:www.jb51.net # date:2013/2/28 myPath=/var/log/httpd/ myFile=/var /log/httpd/access.log # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x $myPath]; then ...
在SQL Server中可以使用系统内部存储过程xp_fileexist判断文件是否存在,如果存在再使用xp_cmdshell删除文件。xp_fileexist除了可以判断文件是否存在外,还可以判断文件夹是否存在,下面是下使用这两个的示例。 删除...
(1)判断/home下是否存在一个的目录文件;如果不存在则转(2)继续往下执行;如果存在则判断该目录下是否存在一个.c文件,如果不存在则转第(3)步,如果存在则从第(4)步继续执行 (2)在/home下创建一个目录;...
Linux判断文件和文件夹是否存在shell判断文件,目录是否存在或者具有权限这里的-x 参数判断$myPath是否存在并且是否具有可执行权限这里的-d 参数判
#这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x $myPath]; then mkdir $myPath fi #这里的-d 参数判断$myPath是否存在 if [ ! -d $myPath]; then mkdir $myPath fi #这里的-f参数判断$myFile...
主要介绍了bash判断文件或文件夹是否存在的shell代码,需要的朋友可以参考下
read -ep 请输入判断的文件类型: type case $type in b) if [ -b $file ];then echo 是一个block文件! else echo 不是一个block文件! fi ;; c) if [ -c $file ];then echo 是一个字符设备文件! else
主要介绍了Shell脚本统计当前目录下目录和文件的数量,Linux下如何统计当前目录下文件有多少个,目录又有多少个呢,使用本文脚本即可实现,需要的朋友可以参考下
shell编程中使用到得if语句内判断参数 –b当file存在并且是块文件时返回真 -c当file存在并且是字符文件时返回真 -d当pathname存在并且是一个目录时返回真 -e当pathname指定的文件或目录存在时返回真 -...
如果你是习惯于Windows命令...你所熟悉的DOS命令再Linux中基本不存在。摆在你面前的是一大堆要记背的命令。一种替代方案是利用强大的Linux外壳命令编写shell脚本,让你在Linux下也能用DOS命令。本文将为大家具体介绍。
个脚本可以实现指定目录下同名但不同后缀名的查找,可以拓展为删除指定的文件的脚本,觉得很实用,分享一下
本人自己写的简单的文件拷贝脚本,BAT格式(1、判断文件是否存在,不存在即拷贝到指定目录,存在不执行任何操作),可以直接记事本打开编辑,自定义目录