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

相关推荐
s65166549613 小时前
Linux内核学习-汇编笔记
linux
IMPYLH13 小时前
Linux 的 csplit 命令
linux·运维·服务器·数据库
钰衡大师13 小时前
Nohup 使用技术文档
linux·服务器·运维开发·unix
Sakuyu4346813 小时前
zabbix源码安装
linux·运维·zabbix
赖亦无13 小时前
【水动力学】06 Linux + Conda 环境下源码编译安装 pypims (CUDA加速) 避坑指南
linux·运维·conda·pypims·水动力学
穷人小水滴14 小时前
使用 WebRTC 实现局域网投屏: PC (GNOME ArchLinux) -> 平板 (Android)
android·linux·webrtc·浏览器·js·gnome·投屏
恋红尘14 小时前
K8S 控制器-资源调度-叩丁狼
linux·docker·kubernetes
weixin_5375904514 小时前
《C程序设计语言》练习答案(练习1-7)
linux·c语言·算法
123过去16 小时前
ike-scan使用教程
linux·测试工具
疯狂吧小飞牛1 天前
GPG基础指令
linux·服务器·网络