转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html

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脚本判断文件类型的更多相关文章

  1. 【原】Shell脚本-判断文件有无进而复制

    2016年7月5日某同学在群上求助要编一个判断文件或目录在某路径下有无进而有的就复制粘贴到另一路径下,无的则将代码中断(不往下执行命令)的脚本.逐一完善.模板如下(生产环境可用到路径环境变量) --- ...

  2. 使用shell脚本查看文件类型

    显示文件类型 #如查看 /etc 目录 [root@localhost ~]# sh test.sh /etc /etc/ [目录文件] #如查看 /etc 目录下所有文件 [root@localho ...

  3. shell判断文件类型和权限

    shell  判断文件类型. -d 文件 判断该文件是否存在,并且是否为目录(是目录为真) -e文件 判断该文件是否存在(存在为真) -f文件 判断该文件是否存在,并且是否为文件(是普通文件为真) - ...

  4. Shell脚本 判断

    #Linux系统Shell脚本判断变量文件目录:权限.是否存在.空值.相等 1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真str1 != str2 当串str1和str2不等时 ...

  5. JavaScript根据文件名判断文件类型

    //JavaScript根据文件名判断文件类型 var imgExt = new Array(".png",".jpg",".jpeg",& ...

  6. 利用PHP取二进制文件头判断文件类型

    <?php $files = array('D:\no.jpg', 'D:\no.png','D:\no2.JPEG','D:\no.BMP'); $fileTypes = array( 779 ...

  7. Linux中用st_mode判断文件类型

    Linux中用st_mode判断文件类型 2012-12-11 12:41 14214人阅读 评论(4) 收藏 举报  分类: Linux(8)  C/C++(20)  版权声明:本文为博主原创文章, ...

  8. php 读取文件头判断文件类型的实现代码

    php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...

  9. PHP取二进制文件头快速判断文件类型的实现代码

    通过读取文件头信息来识别文件的真实类型. 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别,PHP扩展中提供了类似 exif_im ...

随机推荐

  1. 遍历Map集合的方法

    创建一个MAP的栗子: Map<String, Integer> tempMap = new HashMap<String, Integer>(); tempMap.put(& ...

  2. [原创] hadoop学习笔记:卸载和安装jdk

    一,卸载jdk 1.确定jdk版本 #rpm -qa  | grep jak 可能的结果: java-1.7.0-openjdk-1.7.0.75-2.5.4.2.el7_0.x86_64 java- ...

  3. C++之路进阶——bzoj2199(奶牛议会)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  4. 线性表基本维护[ACM]

    #include "iostream" #include "string" using namespace std; typedef struct node{ ...

  5. paper 30 :libsvm的参数说明

    English: libsvm_options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-c ...

  6. sdf

    SDF(Standard Delay Format)是一种存储timing data的文件,其中的数据是tool-independent的 可以包括: 1)Delay: module path, de ...

  7. ahb2apb和apb2apb async bridge

    AHB 3.0目前不支持security world. AHB到APB的async bridge主要包括三个部分: 1)AHB domain 1)产生信号hactive = HSEL & HT ...

  8. 3. 星际争霸之php设计模式--简单工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  9. [Ubuntu] ubuntu13.04 从php5.4降级到php5.3

    ubuntu12.10以后,默认的deb安装库上面的php版本已经是5.4了,公司的项目使用5.4的时候,还是会出现很多问题,所以不得不降级安装5.3 顺便说一句,我原来的环境是nginx + php ...

  10. opencv的一次性配置

    最近做自然场景中的文字识别,想尝试些图像处理方法,感觉每一种方法都需要自己写很麻烦,自然就想到了强大的开源的跨平台计算机视觉库OpenCv.我用的是opencv2.4.9版本,VS用的是2010,他们 ...