shell 判断文件是否存在(csh bash)

文章目录

    • 前言
      • [1. -e 判断文件是否存在](#1. -e 判断文件是否存在)
      • [2. -f 判断文件是否存在且为普通文件](#2. -f 判断文件是否存在且为普通文件)
      • [3. -d 判断文件是否存在且为目录](#3. -d 判断文件是否存在且为目录)
      • [3. -s 判断文件是否存在且不为空](#3. -s 判断文件是否存在且不为空)
      • [4. -r 判断文件是否存在且可读](#4. -r 判断文件是否存在且可读)
      • [5. -w 判断文件是否存在且可写](#5. -w 判断文件是否存在且可写)
      • [6. -x 判断文件是否存在且可执行](#6. -x 判断文件是否存在且可执行)

前言

Shell 编程能提升我们工作效率,在 shell 中, 可以借助文件测试符号来判断一个文件是否存在。 常用的文件测试符号有 -e, -f, -d, -s, -r。

1. -e 判断文件是否存在

bash 复制代码
#!/bin/csh

set fileName = "test.txt"
#set fileName = "test_dir"

if (-e $fileName) then
  echo "$fileName" " is exit"
else
  echo "$fileName" " is not exit"
endif
bash 复制代码
#!/bin/bash

file=test.txt
#file=test_dir

if [ -e "$file" ]; then
  echo "$file" "is exit"
else
  echo "$file" "is not exit"
fi

2. -f 判断文件是否存在且为普通文件

3. -d 判断文件是否存在且为目录

bash 复制代码
#!/bin/csh

set fileName = "test_test"

if (-d $fileName) then
  echo "$fileName" " is exit"
else
  echo "$fileName" " is not exit"
endif

3. -s 判断文件是否存在且不为空

4. -r 判断文件是否存在且可读

5. -w 判断文件是否存在且可写

6. -x 判断文件是否存在且可执行

相关推荐
开开心心就好2 小时前
定时结束进程的小工具,到点还能关机
linux·运维·服务器·智能手机·pdf·电脑·word
风翼靓崽2 小时前
ansible
linux·运维·ansible
遨游DATA2 小时前
容器退出码 126 排查:静态资源镜像层如何改写目录权限
linux·docker·kubernetes·文件权限
笔记本note3 小时前
Linux 定时器与时间管理
linux·时间·定时器
NPE~3 小时前
.sock 文件入门:从“文件路标”到进程间通信
linux·macos·教程·基础知识·.sock
做嵌入式网络的老骆3 小时前
01|VSS 400MB 就是内存泄漏吗?搞懂 Linux VSS、RSS、PSS 进程内存
linux
cpolar技术支持4 小时前
rsync -a 漏同步,-c 被 --update 吃掉
linux·备份·rsync·cpolar·数据校验
女神下凡5 小时前
芯易选(152):炬芯(Actions)_开发指南
linux·arm开发·stm32·单片机·嵌入式硬件
qetfw8 小时前
EVE-NG 导入 Debian 虚拟机:VMDK 转 QCOW2 与启动验证
linux·前端·debian·github
master3368 小时前
Linux 用户与权限体系:由浅入深完全指南
linux·运维·服务器