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 这个区别是很大的!
- 浏览: 1231751 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (461)
- 心得体会 (166)
- Hibernate (9)
- Spring (12)
- Struts1 (3)
- Ajax (4)
- Java (54)
- 其他技术 (21)
- 数据库 (29)
- EXT (0)
- Struts2 (7)
- Xml (3)
- HTML (5)
- JavaScript (12)
- 面试相关 (3)
- BLOG (11)
- 计算机 (11)
- PMP (0)
- OGNL (1)
- LINUX (79)
- AIX (1)
- Ubuntu (14)
- Android (1)
- hadoop (3)
- LINUX debian (3)
- 心得体会 eclipse (2)
- JSTL (1)
- 心得体会 hadoop cdh3u5 (2)
- maven (5)
- Hive (1)
- 心得体会 工具使用 (3)
- spring data jpa Query By Example(QBE) (1)
- nginx (2)
- Apache (1)
- mysql (6)
- LINUX mysql (2)
- freemaker (1)
- 心得体会 FastDFS Nginx 断点续传 (1)
- LINUX FastDFS Nginx 断点续传 (1)
- 心得体会 Mybatis (2)
- 心得体会 mysql (4)
- php (1)
- logback 简介 (5)
- EL (1)
- Tomcat (2)
- win7 (1)
- LINUX maven (1)
- scrumworks (1)
- linux nginx (6)
- svn linux (1)
- mac (3)
- mac git (1)
- git (1)
- nexus (2)
- golang (1)
- LINUX Redis (1)
- mac oracle (1)
最新评论
-
a785975139:
有用
MySQL Error :SHOW PROFILES -
yijiulove:
弄了半天,参照你的方法解决了.特来感谢,知道可能是先加载,但是 ...
Spring和Mybatis整合时无法读取properties的处理方案 -
chenjinqi1987:
Missing com.sun.jdmk:jmxtools:jar:1.2.1 -
leifeng2:
请问怎么使用,运行之后d盘符没有生产音频文件呢?
java录音程序 -
sundful:
chenghong726 写道你好,我也遇到你这样的问题,按照 ...
Spring和Mybatis整合时无法读取properties的处理方案
发表评论
-
CentOS6.5系统更改yum源为163
2016-11-16 16:49 6611、备份系统源 # cd /etc/yum.repos.d ... -
shell脚本----if(数字条件,字符串条件,字符串为空)
2016-04-07 17:58 1398二元比较操作符,比较变量或者比较数字. 注意数字与字符串的 ... -
shell bash判断文件或文件夹是否存在
2016-03-25 09:08 2007#shell判断文件夹是否存在 #如果文件夹不存 ... -
Linux保持SSH连接时间设置
2015-12-02 09:33 1002连接SSH服务器刚刚离开一会就断开,不得不需要反复连接服务器 ... -
Centos安装Vsftpd
2015-11-03 19:10 836安装部分摘自开源中国,最后红色部分解决无法登陆(cannot ... -
Linux使用jstat命令查看jvm的GC情况
2015-09-09 17:20 2174命令格式 jstat命令命令格式: jstat [Op ... -
如何从CPAN安装Perl模块
2015-06-11 11:20 1107Perl有一个从外部导入Perl模块的机制,从外部导入到Pe ... -
ubuntu - 下更改语言环境
2015-05-05 14:56 9282.server 对于使用ubuntu server版的朋友 ... -
crontab命令的使用方法
2015-04-29 19:15 619crontab命令常见于Unix和Linux的操作系统之中, ... -
关于nginx的启动和停止、平滑重启
2015-04-29 16:01 1750一、nginx的启动 /usr/local/nginx/s ... -
CentOS修改DNS地址方法
2015-04-24 17:02 753DNS是用于解析域名后端IP的服务器。Linux和Windo ... -
Ubuntu 修改locale
2015-02-07 12:30 746vim /etc/default/locLANG=" ... -
Ubuntu 12 ulimit 系统最大打开文件个数 设置
2015-02-07 12:28 941配置/etc/profile 最后一行加上 # u ... -
crontab ubuntu
2015-02-05 11:48 987第一次使用crontab 时,会出现 no crontab ... -
tar打包排除某个目录
2015-02-05 11:47 1625tar zcvf fd.tar.gz * --exclu ... -
Ubuntu下安装nginx
2015-01-16 11:49 19701)、下载 sudo wget http://ngi ... -
五种方法实现Linux批量重命名文件
2014-11-17 18:35 2198Linux批量重命名文件会涉及到改变一个字母、改变一些相连 ... -
linux 解压war包
2014-10-14 08:28 881网上很多人说用jar包解 ... -
centos6.4-ssh-POSSIBLE BREAK-IN ATTEMPT
2014-09-13 14:34 1136[root@rd-1 yum.repos.d]# scp Ce ... -
CentOS 中ulimit值调整并永久生效的方法
2014-08-28 11:56 2782echo -ne " * soft nofil ...
相关推荐
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、判断文件是否存在,不存在即拷贝到指定目录,存在不执行任何操作),可以直接记事本打开编辑,自定义目录