如何在Bash中判断一个文件是否存在?
这段代码用于检查文件是否存在:
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
echo "File $FILE exists."
else
echo "File $FILE does not exist."
fi
如何仅检查文件不存在?
test 命令(这里写作 [)有一个 "not" 逻辑运算符,即感叹号 !:
if [ ! -f /tmp/foo.txt ]; then
echo "File not found!"
fi

浙公网安备 33010602011771号