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

相关推荐
蒸蒸yyyyzwd23 分钟前
后端学习笔记 day4
linux·笔记·学习
upp1 小时前
[最新版本centos 10系统制作与安装]
linux·运维·centos
ShineWinsu1 小时前
对于Linux:进程优先级、进程切换以及进程调度的解析
linux·面试·笔试·进程·进程切换·进程调度·进程优先级
Kira Skyler3 小时前
kprobe函数入口时的汇编跳板执行流程与栈帧机制
linux·汇编
桌面运维家3 小时前
VHD/VHDX 数据守护:BAT位图校验与修复
linux·服务器·网络
pupudawang3 小时前
Linux下安装Nginx服务及systemctl方式管理nginx详情
linux·运维·nginx
零K沁雪3 小时前
Linux 内核遍历宏介绍
linux·内核
淼淼爱喝水3 小时前
openEuler 下 Ansible 基础命令详解与实操演示2
linux·运维·windows
拾贰_C4 小时前
【Ubuntu | install | 安装软件】 Ubuntu软件安装多种方式以及卸载
linux·运维·ubuntu
·醉挽清风·4 小时前
学习笔记—Linux—信号阻塞&信号捕捉
linux·笔记·学习