Warm tip: This article is reproduced from serverfault.com, please click

其他-如何判断Bash中是否不存在常规文件?

(其他 - How do I tell if a regular file does not exist in Bash?)

发布于 2009-03-12 14:48:43

我使用以下脚本来查看文件是否存在:

#!/bin/bash

FILE=$1     
if [ -f $FILE ]; then
   echo "File $FILE exists."
else
   echo "File $FILE does not exist."
fi

如果我只想检查文件是否存在,使用的正确语法是什么

#!/bin/bash

FILE=$1     
if [ $FILE does not exist ]; then
   echo "File $FILE does not exist."
fi
Questioner
Bill the Lizard
Viewed
11
John Feminella 2014-06-13 22:55:36

测试命令([在这里)有一个“非”逻辑运算符是感叹号(类似于许多其他语言)。试试这个:

if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi