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

相关推荐
今天也想躺ping37 分钟前
linux系统移植pjsua库实现sip通话功能
linux·sip·交叉编译
hkj88081 小时前
Linux 总线-设备-驱动(Bus-Device-Driver)完整协作原理
linux·运维·microsoft
早点睡啊Y2 小时前
深入学 LangChain 官方文档(一):总览、安装与快速开始
linux·服务器·langchain
一直在努力学习的菜鸟2 小时前
Rocky8.5编译安装PHP8.4
linux·运维
国产化创客3 小时前
Kindle完整越狱改造:从闲置泡面盖到Linux开发与智能家居终端
linux·运维·物联网·嵌入式·智能家居·智能硬件
qetfw3 小时前
CentOS 7 搭建 MariaDB 数据库服务
linux·数据库·centos·mariadb
皮卡狮4 小时前
进程的地址空间
linux
Darkwanderor4 小时前
动、静态库相关内容的详细介绍
linux·c语言·开发语言·c++
2401_858286116 小时前
OS79.【Linux】POSIX信号量
linux·运维·服务器
CodingPioneer6 小时前
03零刻Pro-LS2K3000-Loongnix 20.7.rc1硬盘分区、格式化、挂载与卸载
linux·运维·服务器·硬盘挂载