转自: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. 数据库迁移之从oracle 到 MySQL

    方式一: 手动方式导入导出 手动的方式导入, 就是操作步骤会比较繁琐一些. 对Table 的结构和数据: 1. 使用 SQL Developer 把 oracle 的 table 的schema 和 ...

  2. 转:python webdriver API 之alert/confirm/prompt 处理

    webdriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到 alert/conf ...

  3. 树形DP+RMQ+单调队列(Bob’s Race HDU4123)

    题意:有n个房子,这些房子被n-1条道路连接,有一些运动员从一个房子为起点尽可能跑最远的距离且不能通过一条道路超过两次,这些运行员不能选择同样的起点,这些运动员跑的最远距离和最近距离的差值不能超过Q, ...

  4. Java基础(3):求任意数字的长度的三种方法

    题目:统计任意一个数字的长度 方法一:先把数字转换为字符换,再直接用现成的函数统计长度即可 方法二:用API public class Test { final static int[] sizeTa ...

  5. poj: 1004

    简单题 #include <iostream> #include <stdio.h> #include <string.h> #include <stack& ...

  6. HDU 4513 吉哥系列故事——完美队形II(Manacher)

    Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...

  7. LED流水灯(二)

    记住看汇编的时候是红在上面 黑色在下面 startup.s 程序 ; MDK跑马灯实验; PRESERVE8               // 字节对齐关键词 ,汇编有8位对齐的要求,要添加 AREA ...

  8. ligerui_ligerTree_001_第一个“树”效果

    折叠.展开.有复选框.没有复选框: 源码地址:http://download.csdn.net/detail/poiuy1991719/8571255 效果图: <%@ page languag ...

  9. 找到多个与名为“Index”的控制器匹配的类型的解决方法!

    “/”应用程序中的服务器错误. 找到多个与名为“Index”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由在搜索匹配此请求的控制器时没有指定 ...

  10. Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别

    Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12  CSDN博客原文  http://blog.cs ...