当前位置:首页 > 编程笔记 > 正文
已解决

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

来自网友在路上 184884提问 提问时间:2023-11-21 09:11:07阅读次数: 84

最佳答案 问答题库848位专家为你答疑解惑

文章目录

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

前言

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

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

#!/bin/cshset fileName = "test.txt"
#set fileName = "test_dir"if (-e $fileName) thenecho "$fileName" " is exit"
elseecho "$fileName" " is not exit"
endif
#!/bin/bashfile=test.txt
#file=test_dirif [ -e "$file" ]; thenecho "$file" "is exit"
elseecho "$file" "is not exit"
fi

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

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

#!/bin/cshset fileName = "test_test"if (-d $fileName) thenecho "$fileName" " is exit"
elseecho "$fileName" " is not exit"
endif

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

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

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

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

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"shell 判断文件是否存在(csh bash)":http://eshow365.cn/6-41089-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!