这段代码用于检查文件是否存在:
#!/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