shell脚本怎样判断文件是否存在

2023-12-05 0 1,025
目录
  • shell脚本判断文件是否存在
  • shell脚本之文件是否存在、权限校验
    • 判断目录是否存在
    • 判断文件是否存在
    • 判断目录/文件是否存在
    • 判断文件权限
    • 判断文件是否属于当前用户
    • 判断文件是否与当前用户相同用户组
    • 比较文件之间是否为新建
    • 复合条件判断文件
  • 总结

    shell脚本判断文件是否存在

    在进行linux系统相关应用程序开发时,少不了要书写一些shell脚本,有时候要用到判断文件或者目录是否存在的脚本,本文笔者做一下笔记,已备后查。

    shell判断文件是否存在的脚本如下:

    //[ 与 ] 的前后必须有空格符
    if [ -f /path/file.ext ]
    then
    echo \”The file exist\”
    else
    echo \”The file doesn\’t exist\”
    fi
    //判断某链接是否存在
    if [ -L /path/link ]
    then
    echo \”The link exist\”
    else
    echo \”The link doesn\’t exist\”
    fi

    其实shell对于文件册测试有好几种选项开关

    现在例举如下:

    表达式测试含义-a filepathfile exists. all files type-b filepathfile exists and is a block special file.-c filepathfile exists and is a character special file.-d filepathfile exists and is a directory.-e filepathfile exists (等同于 -a).-f filepathfile exists and is a regular file.-g filepathfile exists and has its setgid(2) bit set.-G filepathfile exists and has the same group ID as this process.-k filepathfile exists and has its sticky bit set.-L filepathfile exists and is a symbolic link.-n filepathstring length is not zero.-o filepathNamed option is set on.-O filepathfile exists and is owned by the user ID of this process.-p filepathfile exists and is a first in, first out (FIFO) special file ornamed pipe.-r filepathfile exists and is readable by the current process.-s filepathfile exists and has a size greater than zero.-S filepathfile exists and is a socket.-t filepathfile descriptor number fildes is open and associated with aterminal device.-u filepathfile exists and has its setuid(2) bit set.-w filepathfile exists and is writable by the current process.-x filepathfile exists and is executable by the current process.

    shell脚本之文件是否存在、权限校验

    判断目录是否存在

    #判断目录是否存在,判断非加!号, [ ! -d \’/home\’ ]
    if [ -d \’/home\’ ]
    then
        echo \”目录/home存在==========\”
    else
        echo \”目录/home不存在=========\”
    fi

    判断文件是否存在

    #判断文件是否存在
    if [ -f \’/home/docker.log\’ ]
    then
        echo \”文件/home/docker.log存在=============\”
    else
        echo \”文件/home/docker.log不存在===========\”
    fi

    判断目录/文件是否存在

    #判断文件是否存在,目录或文件存在都成立
    if [ -e \’/home\’ ]
    then
        echo \”/home存在==============\”
    else
        echo \”/home不存在============\”
    fi

    判断文件权限

    #检测文件是否可读 -r ,可写 -w ,可执行 -x
    if [ -r \’/home/script/file.log\’ ]
    then
        echo \”文件/home/script/file.log存在并可读==============\”
    else
        echo \”目录/home/script/file.log不存在或不可读==================\”
    fi

    判断文件是否属于当前用户

    #检测文件是否属于当前用户
    file_path=/home/script/file.log
    if [ -O $file_path ]
    then
        echo \”文件$file_path属于当前用户=================\”
    else
        echo \”文件$file_path不属于当前用户===============\”
    fi

    判断文件是否与当前用户相同用户组

    #检测文件是否存在,并且默认组与当前用户相同
    file_path=/home/script/file.log
    if [ -G $file_path ]
    then
        echo \”文件$file_path所属组与当前用户相同=================\”
    else
        echo \”文件$file_path所属组与当前用户不相同================\”
    fi

    比较文件之间是否为新建

    #检测文件file1是否比file2新
    file1=/home/script/file.log
    file2=/home/script/file_1.log
    if [ $file1 -nt $file2 ]
    then
        echo \”文件$file1比文件$file2新==============\”
    fi
    if [ $file1 -ot $file2 ]
    then
        echo \”文件$file1比文件$file2旧===============\”
    fi

    复合条件判断文件

    #判断既是文件 又 可读 ,用 && ,或用 ||
    file=/home/script/file.log
    if [ -f $file ] && [ -r $file ]
    then
            echo \”文件$file是文件,并且可读=============\”
    fi

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源网。

    您可能感兴趣的文章:

    • 判断文件是否存在的shell脚本代码
    • linux shell 中判断文件、目录是否存在的方法
    • shell判断文件,目录是否存在或者具有权限的代码
    • 使用Bash Shell检查文件是否存在的方法
    • PowerShell中使用Test-Path命令检查文件或文件夹路径是否存在示例
    • bash判断文件或文件夹是否存在的shell代码
    • Shell判断文件或目录是否存在的脚本(收藏)
    • Linux中Shell脚本判断文件/文件夹是否存在方法

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 linux shell shell脚本怎样判断文件是否存在 https://www.u-9.cn/jiaoben/linuxshell/101449.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务