shell脚本编程中对文件的判断

来源:互联网 发布:黑客编程入门 编辑:程序博客网 时间:2024/05/16 04:33

声明:本文转自w3好奇者。原文地址:http://www.w3layouts.cn/article/article_40.html 。感谢作者的分享!

shell编程中的条件判断表达式与其他语言区别很大,最主要的区别是shell脚本中有很多对于文件有关的判断,比如判断文件的类型,文件的权限等等。

按照文件类型进行判断

blob.png

代码示例:

#!/bin/bashif [ -e a.txt ]    then        echo 'exists'    else        echo 'no exists'fi

按照文件权限进行判断

blob.png

代码示例:

#!/bin/bashif [ -w a.txt ]    then        echo 'read'    else        echo 'no read'fi

两个文件之间进行比较

blob.png

代码示例:

#!/bin/bashif [ a.txt -nt b.txt ]    then        echo 'a.txt is newer than b.txt'    else        echo 'a.txt is older than b.txt'fi


0 0
原创粉丝点击