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 判断文件是否存在且可执行

相关推荐
9分钟带帽5 小时前
linux_系统开机自动执行shell脚本
linux·服务器
嵌入式小能手7 小时前
飞凌嵌入式ElfBoard-进程间的通信之命名管道
linux·服务器·算法
AOwhisky7 小时前
Ceph系列第六期:Ceph 文件系统(CephFS)精讲
linux·运维·网络·笔记·ceph
Starry-sky(jing)8 小时前
# Linux 下 Qt 应用无障碍自动化:记一次wx无人值守系统的架构演进
linux·qt·自动化
荒--8 小时前
apt dpkg 命令详解
linux·服务器
嵌入式小能手9 小时前
飞凌嵌入式ElfBoard-进程间的通信之信号的发送alarm
linux
用户23678298016810 小时前
Linux systemctl 服务管理命令:从 systemd 架构到实战技巧
linux
LIZHUOLONG110 小时前
linux 设备初始化
linux·运维·服务器
雪霁清寒11 小时前
麒麟V10用MobaXterm远程连接SSH偶尔卡顿的问题
linux·ssh
ylscode11 小时前
Linux CIFSwitch 内核新漏洞允许攻击者获得 root 权限
linux·运维·服务器