如何在Bash中判断一个文件是否存在?

内容来自 DOC https://q.houxu6.top/?s=如何在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
posted @ 2023-10-21 21:54  小满独家  阅读(133)  评论(0)    收藏  举报