Bash 检查文件是否存在且不为空

语言:
Shell
8 浏览
0 收藏
1小时前

代码实现

Shell
#!/bin/bash

FILE="data.txt"

if [ -s "$FILE" ]; then
  echo "File exists and is not empty."
else
  echo "File does not exist or is empty."
fi

在 Bash 脚本中,经常需要检查某个文件是否存在并且非空。

#bash#脚本#检查文件

片段说明

  • -s 参数表示文件存在且大小大于 0
  • 如果文件不存在或为空,则进入 else 分支
  • 可用于定时任务、日志监控等场景

评论

加载中...