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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/638975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:01:34  来源:igfitidea点击:

How do I tell if a regular file does not exist in Bash?

bashfile-ioscripting

提问by Bill the Lizard

I've used the following script to see if a file exists:

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

#!/bin/bash

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

What's the correct syntax to use if I only want to check if the file does notexist?

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

#!/bin/bash

FILE=     
if [ $FILE does not exist ]; then
   echo "File $FILE does not exist."
fi

回答by John Feminella

The testcommand ([here) has a "not" logical operator which is the exclamation point (similar to many other languages). Try this:

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

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

回答by BlueCacti

Bash File Testing

Bash 文件测试

-b filename- Block special file
-c filename- Special character file
-d directoryname- Check for directory Existence
-e filename- Check for file existence, regardless of type (node, directory, socket, etc.)
-f filename- Check for regular file existence not a directory
-G filename- Check if file exists and is owned by effective group ID
-G filename set-group-id- True if file exists and is set-group-id
-k filename- Sticky bit
-L filename- Symbolic link
-O filename- True if file exists and is owned by the effective user id
-r filename- Check if file is a readable
-S filename- Check if file is socket
-s filename- Check if file is nonzero size
-u filename- Check if file set-user-id bit is set
-w filename- Check if file is writable
-x filename- Check if file is executable

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

How to use:

如何使用:

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

A test expressioncan be negated by using the !operator

测试表达可以通过使用被否定!操作者

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

回答by BlueCacti

You can negate an expression with "!":

你可以用“!”否定一个表达式:

#!/bin/bash
FILE=

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

The relevant man page is man testor, equivalently, man [-- or help testor help [for the built-in bash command.

相关的手册页是man test或,等效地,man [-- 或help testhelp [用于内置 bash 命令。

回答by guns

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

Also, it's possible that the file is a broken symbolic link, or a non-regular file, like e.g. a socket, device or fifo. For example, to add a check for broken symlinks:

此外,该文件可能是损坏的符号链接或非常规文件,例如套接字、设备或 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

回答by Elazar Leibovich

It's worth mentioning that if you need to execute a single command you can abbreviate

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

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

to

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

or

或者

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

回答by J. M. Becker

I prefer to do the following one-liner, in POSIXshell compatible format:

我更喜欢以POSIXshell 兼容格式执行以下单行操作:

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

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

For a couple of commands, like I would do in a script:

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

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

Once I started doing this, I rarely use the fully typed syntax anymore!!

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

回答by SD.

To test file existence, the parameter can be any one of the following:

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

-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

All the tests below apply to regular files, directories, and symlinks:

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

-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

Example script:

示例脚本:

#!/bin/bash
FILE=

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

回答by Jahid

You can do this:

你可以这样做:

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

or

或者

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

If you want to check for file and folder both, then use -eoption instead of -f. -ereturns true for regular files, directories, socket, character special files, block special files etc.

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

回答by artdanil

You should be careful about running testfor an unquoted variable, because it might produce unexpected results:

运行test未加引号的变量时应该小心,因为它可能会产生意想不到的结果:

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

The recommendation is usually to have the tested variable surrounded by double quotation marks:

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

#!/bin/sh
FILE=

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

回答by artdanil

There are three distinct ways to do this:

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

  1. Negate the exit status with bash (no other answer has said this):

    if ! [ -e "$file" ]; then
        echo "file does not exist"
    fi
    

    Or:

    ! [ -e "$file" ] && echo "file does not exist"
    
  2. Negate the test inside the test command [(that is the way most answers before have presented):

    if [ ! -e "$file" ]; then
        echo "file does not exist"
    fi
    

    Or:

    [ ! -e "$file" ] && echo "file does not exist"
    
  3. Act on the result of the test being negative (||instead of &&):

    Only:

    [ -e "$file" ] || echo "file does not exist"
    

    This looks silly (IMO), don't use it unless your code has to be portable to the Bourne shell (like the /bin/shof Solaris 10 or earlier) that lacked the pipeline negation operator (!):

    if [ -e "$file" ]; then
        :
    else
        echo "file does not exist"
    fi
    
  1. 用 bash 否定退出状态(没有其他答案说过这个):

    if ! [ -e "$file" ]; then
        echo "file does not exist"
    fi
    

    或者:

    ! [ -e "$file" ] && echo "file does not exist"
    
  2. 否定测试命令中的测试[(这是之前大多数答案的呈现方式):

    if [ ! -e "$file" ]; then
        echo "file does not exist"
    fi
    

    或者:

    [ ! -e "$file" ] && echo "file does not exist"
    
  3. 根据测试结果是否定的(||而不是 )采取行动&&

    仅有的:

    [ -e "$file" ] || echo "file does not exist"
    

    这看起来很傻 (IMO),除非您的代码必须可移植到/bin/sh缺少管道否定运算符 ( !)的 Bourne shell(如Solaris 10 或更早版本),否则不要使用它:

    if [ -e "$file" ]; then
        :
    else
        echo "file does not exist"
    fi