• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > 使用Bash Shell检查文件是否存在的方法

使用Bash Shell检查文件是否存在的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了bash shell,bash shell是什么,bash shell教程,win10 bash shell,linux bash shell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

前言

大家在工作的时候可能经常会遇到这样的需求,在类Unix系统的Bash环境下,怎样检查文件是否存在呢?既然有需求,当然就有解决的办法了,Shell中的test命令,可以用来检测文件的类型或者比较数值是否相等,该命令也能用来检查文件是否存在。

可以用如下的命令来进行检查:

test -e filename
[ -e filename ]
 
test -f filename
[ -f filename ]
</div>

下面的命令,则使用Shell的条件表达式,判断/etc/hosts文件是否存在:

[ -f /etc/hosts ] && echo "Found" || echo "Not found"
</div>

该组合命令会输出以下内容:

Found
</div>

更常见的用法,则是将test命令放置在if..else..fi条件判断的条件表达式,然后在其中写上不同的分支逻辑

#!/bin/bash
file="/etc/hosts"
if [ -f "$file" ]
then
 echo "$file found."
else
 echo "$file not found."
fi
</div>

检测文件属性的相关操作符

如果文件存在,并且具有相应的属性,如下的操作符都会返回true:

  -b FILE
    FILE exists and is block special
  -c FILE
    FILE exists and is character special
  -d FILE
    FILE exists and is a directory
  -e FILE
    FILE exists
  -f FILE
    FILE exists and is a regular file
  -g FILE
    FILE exists and is set-group-ID
  -G FILE
    FILE exists and is owned by the effective group ID
  -h FILE
    FILE exists and is a symbolic link (same as -L)
  -k FILE
    FILE exists and has its sticky bit set
  -L FILE
    FILE exists and is a symbolic link (same as -h)
  -O FILE
    FILE exists and is owned by the effective user ID
  -p FILE
    FILE exists and is a named pipe
  -r FILE
    FILE exists and read permission is granted
  -s FILE
    FILE exists and has a size greater than zero
  -S FILE
    FILE exists and is a socket
  -t FD file descriptor FD is opened on a terminal
  -u FILE
    FILE exists and its set-user-ID bit is set
  -w FILE
    FILE exists and write permission is granted
  -x FILE
    FILE exists and execute (or search) permission is granted
</div>

以上命令,从man test复制而来。

使用上述符号的方法一模一样:

if [ operator FileName ]
then
  echo "FileName - Found, take some action here"
else
 echo "FileName - Not found, take some action here"
fi
</div>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可留言交流。

</div>

您可能想查找下面的文章:

  • bash判断文件或文件夹是否存在的shell代码
  • 使用Bash Shell检查文件是否存在的方法
  • Bash中数组的操作教程
  • Bash Shell中的select命令简单使用示例
  • 使用Bash Shell获取文件名和目录名的简单方法
  • 使用Bash Shell对目录中的文件实现递归式拷贝
  • Linux bash Shell中的变量类型详解
  • Bash Shell中忽略大小写的设置方法
  • Shell 编程:Bash空格的那点事
  • Bash Shell 注释多行的几种方法

相关文章

  • shell脚本实现监控shell脚本的执行流程及变量的值
  • Shell脚本实现启动PHP内置FastCGI Server
  • 检查linux网络状态的两个脚本
  • shell编程基础之认识与学习BASH
  • Shell脚本批量重命名文件后缀的3种实现
  • 用来检测输入的选项$1是否在PATH中的shell脚本
  • Shell脚本实现监控MySQL主从同步
  • linux下编译boost.python简单方法
  • Shell脚本统计文件行数的8种方法
  • Linux 下xargs命令详解及xargs与管道的区别

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 让代码整洁、过程清晰的BASH Shell编程技巧
    • shell脚本监控linux系统内存使用情况的方法(不使用nagios监控linux)
    • bash 循环中变量作用范围的问题分析
    • linux自动清理日志脚本分享
    • Ubuntu配置NFS的具体流程(推荐)
    • 自己常用的一些shell脚本分享
    • 一个简单的防CC攻击Shell脚本分享
    • Shell脚本注释写法
    • 一句话Shell命令关闭不需要的随机启动服务
    • 常用Linux Shell进阶部分小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有