在 Bash 中检查文件是否存在

本教程使用 test 命令检查 bash 中是否不存在常规文件。

在 Bash 中检查文件是否不存在 test

test 命令有一个逻辑非运算符,即 !。我们还将添加 -f 选项来指定文件。在这种情况下,条件测试文件是否存在。

#!/bin/bash
if [ ! -f check_fil.sh ];
then
     printf 'No such file\n'
else
     printf 'File exists\n'
fi

输出:

No such file