shell中判断文件或目录是否存在

可以通过man
test来查看文档,下面的文章转自http://m.blog.csdn.net/blog/yuanjungogogo/9222875


#!/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.

shell中判断文件或目录是否存在,码迷,mamicode.com

时间: 2024-08-02 16:38:41

shell中判断文件或目录是否存在的相关文章

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

一. 具体每个选项对应的判断内容: -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L filename 如果 filename为符号链接,则为真 -r filename 如果 filename可读,则为真 -w filename 如果 filename可写,则为真 -x filename 如果 filename可执行,则为真 -s filename 如果

Shell脚本(脚本中的逻辑判断,逻辑判断表达式,判断文件和目录属性,case判断)

逻辑判断表达式: -gt = 大于    -lt = 小于    -ge = 大于等于   -le = 小于等于   -eq = 恒等于  -ne = 不等于 这里要注意空格 也可以用&&和||结合多个条件 例如1: [[email protected] shell]# cat if4.sh #/bin/bash a=5 if [ $a -gt 4 ] && [ $a -lt 6 ] then echo "4<a<6" else echo n

shell中的逻辑判断,if 判断文件、目录属性,if判断的一些特殊用法

shell中的逻辑判断 格式1:if 条件 ; then 语句; fi //如果满足条件,然后执行语句 [[email protected] shell]# cat if1.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi [[email protected] shell]# sh -x if1.sh + a=5 + '[' 5 -gt 3 ']' + echo ok ok 格式2:if 条件; then 语句; else 语句; fi //如

linux下判断文件和目录是否存在

1.前言 工作中涉及到文件系统,有时候需要判断文件和目录是否存在.我结合APUE第四章文件和目录,总结一下如何正确判断文件和目录是否存在,方便以后查询. 2.stat系列函数 stat函数用来返回与文件有关的结构信息.stat系列函数有三种情况,分别对应文件名称.文件描述符和符号链接文件.stat结构描述了文件的属性,主要包括文件的类型.文件大小等等.详细stat结构如下所示: 1 struct stat { 2 mode_t st_mode; // file type & mode(permi

mint/ubuntu 在终端 / Shell中 打开文件夹

我知道的有两种方法, 都是命令后面加路径(nautilus|pcmanfm dir_path) 方法一: 使用 nautilus 安装:sudo apt-get intstall nautilus 效果:nautilus ~ 方法二: 使用 pcmanfm 安装:sudo apt-get intstall pcmanfm 效果:pcmanfm ~ 不过这两种方法都让我有点不爽,因为和原生系统的不一样,还有一点慢, 现在还可以忍,如果你有更好的方法请留言.(原生系统如下) mint/ubuntu

Java学习-040-级联删除目录中的文件、目录

之前在写应用模块,进行单元测试编码的时候,居然脑洞大开居然创建了一个 N 层的目录,到后来删除测试结果目录的时候,才发现删除不了了,提示目录过长无法删除.网上找了一些方法,也找了一些粉碎机,都没能达到想要的结果,我就简写了一个小应用,用于删除自己脑洞大开创建的级联目录.此小应用没有代码注释,也没有进行容错处理,请大家知悉!哈哈哈哈哈.....若发现有错误,也请告知,我会去修改订正,非常感谢! 闲话少述,直接上码了! 1 /** 2 * Aaron.ffp Inc. 3 * Copyright (

shell bash判断文件或文件夹是否存在

shell bash判断文件或文件夹是否存在 1, #如果文件夹不存在,创建文件夹 folder="log" if [ ! -d "${folder}" ]; then mkdir ${folder} fi 2.判断文件是否存在 file="/var/www/log.txt" # -f 参数判断 $file 是否存在 if [ ! -f "$file" ]; then touch "$file" fi. 3

python—— 文件的打开模式和文件对象方法 &amp; os、os.path 模块中关于文件、目录常用的函数使用方法

引用自"鱼c工作室"     文件的打开模式和文件对象方法  : https://fishc.com.cn/forum.php?mod=viewthread&tid=45279&extra=page%3D1%26filter%3Dtypeid%26typeid%3D403 os.os.path 模块中关于文件.目录常用的函数使用方法: https://fishc.com.cn/forum.php?mod=viewthread&tid=45512&extr

if 判断文件、目录属性

[ -f file ]判断是否是普通文件,是否存在 #!/bin/bash f="/tmp/1.txt" #定义变量 if [ -f $f ] #如果变量f存在,且是个普通文件 then #那么 echo $f exist #echo $f exist else #否则 touch $f #创建这个文件 fi [ -d file ] 判断是否是目录,是否存在 #!/bin/bash f="/tmp/1.txt" #定义变量 if [ -d $f ] #如果变量f存在