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

#!/bin/sh 





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 





-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判断文件,目录是否存在或者具有权限的代码

    核心代码 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的 ...

  2. shell判断文件,目录是否存在或者具有权限 (转载)

    转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...

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

    在linux中判断文件,目录是否存在或则具有的权限,根据最近的学习以及网上的资料,进行了以下的总结: #!/bin/sh myPath="/var/log/httpd/" myFi ...

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

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  5. shell判断文件/目录是否存在

    https://www.cnblogs.com/37yan/p/6962563.html caution!!! if should be end with fi caution!!! there sh ...

  6. shell 判断文件、目录是否存在

    shell判断文件是否存在   1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. m ...

  7. Shell中判断文件,目录是否存在

    一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filena ...

  8. shell判断文件是否存在

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  9. Linux shell判断文件和文件夹是否存在

    shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...

随机推荐

  1. nhibernate Stuty

    https://blog.csdn.net/u010371458/article/details/8995166 翻译版 https://wenku.baidu.com/view/46fa56cea1 ...

  2. Git 常用命令大全-转载

    一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...

  3. JavaScript数值千分位格式化的方法和性能

    瞎掰的前提 前端嘛,经常处理数值和时间. 所以数值和时间的格式化少不了. 最近一直在面试前端, 就出了一个如何给数值添加千分位的面试题. 至于答案,我一直都有一种标准, 一是基于你现有的知识可以实现, ...

  4. MSSQL字符串分割

    CREATE FUNCTION dbo.f_splitstr( @str varchar(8000) )RETURNS @r TABLE(id int IDENTITY(1, 1), value va ...

  5. SQL常见面试题(学生表_课程表_成绩表_教师表)

    表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师 ...

  6. 技术简历写这么写,才能得到BAT面试官们的青睐

    公众号[程序员江湖] 作者陆小凤,985 软件硕士,阿里 Java 研发工程师,在技术校园招聘.自学编程.计算机考研等方面有丰富经验和独到见解,目前致力于分享程序员干货和学习经验,同时热衷于分享作为程 ...

  7. Laravel验证问题记录

    1.当购物车提交时,POST传来一个对象{address:2,item:{ {ksu_id:2,count:2},{ksu_id:2,count:2}, } 验证方法: public function ...

  8. CDH目录

    配置文件都在:/etc/服务名, 看hadoop的classpath |grep conf /etc/hadoop/conf log都在: /var/log/服务名 看scm的log: tail -1 ...

  9. Codeforces 1209D Cow and Snacks

    题目大意 有 $n$ 个不同的糖果,从 $1$ 到 $n$ 编号.有 $k$ 个客人.要用糖果招待客人. 对于每个客人,这些糖果中恰有两个是其最爱.第 $i$ 个客人最爱的糖果编号是 $x_i$ 和 ...

  10. (电脑重置之后)win10在桌面点右键鼠标一直转圈;无法点击桌面图标;

    昨天重置系统之后,发现了这个毛病.桌面任务栏都能正常点击,就是一到桌面,无法点击桌面上面的图标(刚开机的时候还可以点).想新建文件夹来着,一点右键,鼠标即开始转圈圈.本来以为自己能好,结果第二天了还这 ...