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判断文件夹是否存在

#如果文件夹不存在,创建文件夹
if [ ! -d "/myfolder" ]; then
mkdir /myfolder
fi #shell判断文件,目录是否存在或者具有权限 folder="/var/www/"
file="/var/www/log" # -x 参数判断 $folder 是否存在并且是否具有可执行权限
if [ ! -x "$folder"]; then
mkdir "$folder"
fi # -d 参数判断 $folder 是否存在
if [ ! -d "$folder"]; then
mkdir "$folder"
fi # -f 参数判断 $file 是否存在
if [ ! -f "$file" ]; then
touch "$file"
fi # -n 判断一个变量是否有值
if [ ! -n "$var" ]; then
echo "$var is empty"
exit 0
fi # 判断两个变量是否相等
if [ "$var1" = "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi

shell判断条件是否存在的更多相关文章

  1. shell判断条件整理

    1.字符串判断 str1 = str2 当两个字符串串有相同内容.长度时为真 str1 != str2 当字符串str1和str2不等时为真 -n str1 当字符串的长度大于0时为真(串非空) -z ...

  2. shell 判断条件

    [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真. [ ...

  3. Shell脚本IF条件判断和判断条件总结

    转自:http://m.jb51.net/article/56553.htm 这篇文章主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结 ...

  4. shell中条件判断if中的-z到-d的意思

    shell中条件判断if中的-z到-d的意思 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 ...

  5. SHELL学习笔记----IF条件判断,判断条件

    SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外.  if list then           do something here   ...

  6. shell中条件判断语法与判断条件小结

    1. IF条件判断语法: if Athen   dosthelif B   dosthelse   dosthfi 2. 判断条件:   2.1 字符串判断   str1 = str2 当两个串有相同 ...

  7. shell 脚本——判断条件

    在之前的shell语言学习笔记中已经写过shell的几种判断语句及循环语句,也简单的介绍了shell语言判断语句和判断条件.在此再做进一步学习. test命令的测试功能 test命令用于检测系统文件及 ...

  8. Shell IF条件判断解析

    IF条件判断 1.基本语法: if [ command ]; then #符合该条件执行的语句 fi2.扩展语法: if [ command ];then #符合该条件执行的语句 elif [ com ...

  9. Linux shell if条件判断2

    前面介绍linux shell的if判断的语法,现在再补充一点. Linux shell if条件判断1 分支判断结构     if , case   下面两个结构语法,已经在前面有过示例. 结构1: ...

随机推荐

  1. Python试卷

    3.写一个函数,计算一个给定的日期是该年的第几天. def getday(self,y=None,m=None,d=None): date = datetime(y,m,d) days = date. ...

  2. 【WPF】Application应用程序启动

    wpf应用程序在启动的时候会自动创建Main函数并调用Application实例的run(),从而启动Application进程.Main函数在一个App.g.cs文件中,App.g.cs文件的位置在 ...

  3. ARCGIS 10中添加excel点字段生产点shp文件的工具

    菜单栏中——文件——添加数据——添加XY数据——选择excel。 选择x,y和投影(注意这里投影应该是原数据的本身投影,不能直接选择你要转换的投影)完成。

  4. hadoop可能遇到的问题

    1.hadoop运行的原理? 2.mapreduce的原理? 3.HDFS存储的机制? 4.举一个简单的例子说明mapreduce是怎么来运行的 ? 5.面试的人给你出一些问题,让你用mapreduc ...

  5. 关于postgresql——常用操作指令

    创建数据库 CREATE DATABASE test WITH OWNER = postgres ENCODING = 'UTF8'; 进入控制台方法,在postgreSQL的安装目的bin下执行命令 ...

  6. 关于搭建Android环境的时候遇到 'could not find adb.exe!'的问题

    关于'could not find adb.exe'的问题 问题原因: 文件所处位置和Android_home变量指路径不一致 文件路径: 解决方法: 直接将相关文件退拽至变量值的路径下即可 小结:a ...

  7. vs中使用过的扩展和好的nuget库

    扩展 ReAttach ReAttach gives you an easy way to ReAttaching your prior debug targets. ReAttach stores ...

  8. 1026: [SCOI2009]windy数 - BZOJ

    Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数?Inp ...

  9. linux上很方便的上传下载文件工具rz和sz

    linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...

  10. Apache URL rewrite 配置

    下面是Apache的配置过程,可以参考下:1.httpd.conf配置文件中加载了mod_rewrite.so模块,使用虚拟主机 #LoadModule rewrite_module modules/ ...