这将检查文件是否存在:

#!/bin/bash

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

如何仅检查文件是否不存在?


测试命令(此处写为[)有一个“not”逻辑运算符!(感叹号):

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

使用!:否定测试中的表达式([是其别名)

#!/bin/bash
FILE=$1

if [ ! -f "$FILE" ]
then
    echo "File $FILE does not exist"
fi

相关的手册页是man-test,或者,对于内置的bash命令,相当于man[-或help-test或help]。

或者(不常用)您可以使用以下方法否定测试结果:

if ! [ -f "$FILE" ]
then
    echo "File $FILE does not exist"
fi

该语法在“管道”和“复合命令”部分的“man 1 bash”中进行了描述。

[[ -f $FILE ]] || printf '%s does not exist!\n' "$FILE"

此外,该文件可能是一个断开的符号链接,或者是一个非常规文件,例如套接字、设备或fifo。例如,要添加断开符号链接的检查:

if [[ ! -f $FILE ]]; then
    if [[ -L $FILE ]]; then
        printf '%s is a broken symlink!\n' "$FILE"
    else
        printf '%s does not exist!\n' "$FILE"
    fi
fi

值得一提的是,如果需要执行单个命令,可以缩写

if [ ! -f "$file" ]; then
    echo "$file"
fi

to

test -f "$file" || echo "$file"

or

[ -f "$file" ] || echo "$file"

我更喜欢使用POSIX shell兼容格式执行以下一行代码:

$ [ -f "/$DIR/$FILE" ] || echo "$FILE NOT FOUND"

$ [ -f "/$DIR/$FILE" ] && echo "$FILE FOUND"

对于一些命令,就像我在脚本中所做的那样:

$  [ -f "/$DIR/$FILE" ] || { echo "$FILE NOT FOUND" ; exit 1 ;}

一旦我开始这样做,我就很少再使用完全类型化的语法了!!

在为未加引号的变量运行测试时,您应该小心,因为它可能会产生意外的结果:

$ [ -f ]
$ echo $?
0
$ [ -f "" ]
$ echo $?
1

建议通常将测试变量用双引号括起来:

#!/bin/sh
FILE=$1

if [ ! -f "$FILE" ]
then
   echo "File $FILE does not exist."
fi

测试可能也很重要。它对我有用(基于Bash Shell:检查文件是否存在):

test -e FILENAME && echo "File exists" || echo "File doesn't exist"

要反转测试,请使用“!”。这相当于其他语言中的“not”逻辑运算符。试试看:

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

或以稍微不同的方式书写:

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

或者您可以使用:

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

或者,将所有内容放在一起:

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

可以写成(使用“and”运算符:&&):

[ ! -f /tmp/foo.txt ] && echo "File not found!"

看起来像这样更短:

[ -f /tmp/foo.txt ] || echo "File not found!"

此shell脚本还可用于查找目录中的文件:

echo "enter file"

read -r a

if [ -s /home/trainee02/"$a" ]
then
    echo "yes. file is there."
else
    echo "sorry. file is not there."
fi

要测试文件是否存在,参数可以是以下任一项:

-e: Returns true if file exists (regular file, directory, or symlink)
-f: Returns true if file exists and is a regular file
-d: Returns true if file exists and is a directory
-h: Returns true if file exists and is a symlink

以下所有测试都适用于常规文件、目录和符号链接:

-r: Returns true if file exists and is readable
-w: Returns true if file exists and is writable
-x: Returns true if file exists and is executable
-s: Returns true if file exists and has a size > 0

示例脚本:

#!/bin/bash
FILE=$1

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

Bash文件测试

-b filename-阻止特殊文件-c filename-特殊字符文件-d directoryname-检查目录是否存在-e filename-检查文件是否存在,无论其类型(节点、目录、套接字等)-f filename-检查常规文件是否存在,而不是目录-G文件名-检查文件是否存在并且由有效组ID拥有-G文件名set group id-如果文件存在并且是set group id,则为True-k文件名-粘滞位-L文件名-符号链接-O filename-如果文件存在并且由有效用户id拥有,则为True-r filename-检查文件是否可读-S filename-检查文件是否为套接字-s filename-检查文件大小是否为非零-u filename-检查是否设置了文件集用户id位-w filename-检查文件是否可写-x filename-检查文件是否可执行

如何使用:

#!/bin/bash
file=./file
if [ -e "$file" ]; then
    echo "File exists"
else 
    echo "File does not exist"
fi 

可以使用!操作人员

#!/bin/bash
file=./file
if [ ! -e "$file" ]; then
    echo "File does not exist"
else 
    echo "File exists"
fi 

最简单的方法

FILE=$1
[ ! -e "${FILE}" ] && echo "does not exist" || echo "exists"

您可以这样做:

[[ ! -f "$FILE" ]] && echo "File doesn't exist"

or

if [[ ! -f "$FILE" ]]; then
    echo "File doesn't exist"
fi

如果要同时检查文件和文件夹,请使用-e选项而不是-f-对于常规文件、目录、套接字、字符特殊文件、块特殊文件等,e返回true。

此代码也有效。

#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
 echo "File '$FILE' Exists"
else
 echo "The File '$FILE' Does Not Exist"
fi

有三种不同的方法可以做到这一点:

用bash否定退出状态(没有其他答案这么说):如果[-e“$file”];然后echo“文件不存在”传真或:! [-e“$file”]&&echo“文件不存在”在测试命令中否定测试[(这是之前大多数答案给出的方式):如果[!-e“$file”];然后echo“文件不存在”传真或:[!-e“$file”]&&echo“文件不存在”在测试结果为阴性时采取行动(||而不是&&):仅限:[-e“$file”]||echo“文件不存在”这看起来很愚蠢(IMO),除非您的代码必须可移植到缺少管道否定运算符(!)的Bourne shell(如Solaris 10或更早版本的/bin/sh),否则不要使用它:如果[-e“$file”];然后:其他的echo“文件不存在”传真

In

[ -f "$file" ]

[命令对$file中存储的路径执行stat()(而不是lstat())系统调用,如果系统调用成功并且stat()返回的文件类型为“正则”,则返回true。

因此,如果[-f“$file”]返回true,则可以判断该文件确实存在,并且是一个常规文件或符号链接,最终解析为常规文件(或者至少在stat()时是这样)。

但是,如果它返回false(或者如果[!-f“$file”]或![-f“$file“]返回true),则有很多不同的可能性:

文件不存在该文件存在,但不是常规文件(可以是设备、fifo、目录、套接字…)文件存在,但您没有对父目录的搜索权限文件存在,但访问该文件的路径太长该文件是到常规文件的符号链接,但您没有对符号链接解析中涉及的某些目录的搜索权限。…stat()系统调用可能失败的任何其他原因。

简而言之,它应该是:

if [ -f "$file" ]; then
  printf '"%s" is a path to a regular file or symlink to regular file\n' "$file"
elif [ -e "$file" ]; then
  printf '"%s" exists but is not a regular file\n' "$file"
elif [ -L "$file" ]; then
  printf '"%s" exists, is a symlink but I cannot tell if it eventually resolves to an actual file, regular or not\n' "$file"
else
  printf 'I cannot tell if "%s" exists, let alone whether it is a regular file or not\n' "$file"
fi

为了确定文件不存在,我们需要stat()系统调用返回错误代码ENOENT(ENOTDIR告诉其中一个路径组件不是目录,这是另一种情况,我们可以通过该路径判断文件不存在)。不幸的是,[命令没有让我们知道这一点。无论错误代码是ENOENT、EACESS(权限被拒绝)、ENAMETOOLONG或其他什么,它都会返回false。

[-e“$file”]测试也可以使用ls-Ld--“$file“>/dev/null完成。在这种情况下,ls将告诉您stat()失败的原因,尽管这些信息不能很容易地以编程方式使用:

$ file=/var/spool/cron/crontabs/root
$ if [ ! -e "$file" ]; then echo does not exist; fi
does not exist
$ if ! ls -Ld -- "$file" > /dev/null; then echo stat failed; fi
ls: cannot access '/var/spool/cron/crontabs/root': Permission denied
stat failed

至少ls告诉我它失败并不是因为文件不存在。这是因为它无法判断文件是否存在。[命令只是忽略了这个问题。

使用zsh shell,您可以在[命令失败后使用$ERRNO特殊变量查询错误代码,并使用zsh/system模块中的$errnos特殊数组解码该数字:

zmodload zsh/system
ERRNO=0
if [ ! -f "$file" ]; then
  err=$ERRNO
  case $errnos[err] in
    ("") echo exists, not a regular file;;
    (ENOENT|ENOTDIR)
       if [ -L "$file" ]; then
         echo broken link
       else
         echo does not exist
       fi;;
    (*) syserror -p "can't tell: " "$err"
  esac
fi

(注意,当使用最新版本的gcc构建zsh时,$errnos支持被一些版本破坏)。

有时使用&&和|运算符可能很方便。

类似于(如果您有命令“test”):

test -b $FILE && echo File not there!

or

test -b $FILE || echo File there!

您还可以在一行中对多个命令进行分组

[-f“文件名”]||(echo test1&&echo test2&&echo test3)

or

[-f“文件名”]| |{echo test1&&echo test2&&echo test3;}

如果文件名不退出,输出将为

test1
test2
test3

注意:(…)在子shell中运行,{…;}在同一个shell中运行。

如果要使用test而不是[],则可以使用!要获得否定:

if ! test "$FILE"; then
  echo "does not exist"
fi
envfile=.env

if [ ! -f "$envfile" ]
then
    echo "$envfile does not exist"
    exit 1
fi