$ touch example.txt
if [[ ! -f example.txt ]]; then
echo "File does not exist or is not a regular file"
else
echo "File exists and is a regular file"
fi
输出:
File exists and is a regular file
如果我们删除 `example.txt` 并尝试再次运行上面的脚本:
$ rm example.txt
if [[ ! -f example.txt ]]; then
echo "File does not exist or is not a regular file"
else
echo "File exists and is a regular file"
fi
输出:
File does not exist or is not a regular file
**使用 `-e`:**
现在,如果我们使用 `-e` 运算符:
$ touch example.txt
if [[ ! -e example.txt ]]; then
echo "File or directory does not exist"
else
echo "File or directory exists"
fi
输出:
File or directory exists
如果我们删除 `example.txt` 并尝试再次运行上面的脚本:
$ rm example.txt
if [[ ! -e example.txt ]]; then
echo "File or directory does not exist"
else
echo "File or directory exists"
fi