检查 bash 中的参数 [文件] 是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25279748/
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-18 11:06:59  来源:igfitidea点击:

Check if arguments[files] exist or not in bash

bashif-statementexit

提问by MLSC

This is part of my script:

这是我的脚本的一部分:

#!/bin/bash

USAGE(){
    echo "Usage: ./`basename 
The file "file1" does not exist!
` <File1> <File2>" } if [ "$#" -ne "2" ]; then USAGE exit 1 fi if [ ! -f "" ]; then echo "The file \"\" does not exist!" exit 1 fi if [ ! -f "" ]; then echo "The file \"\" does not exist!" exit 1 fi

I want to check if file1does not exist prints:

我想检查file1是否不存在打印:

The file "file2" does not exist!

if file2does not exist prints:

如果file2不存在打印:

The files "file1" and "file2" don't exist!

If both does not exist prints:

如果两者都不存在打印:

if [ ! -f "" ] && [ ! -f "" ]; then
    echo "The files \"\" and \"\" do not exist!"
    exit 1
else
    if [ ! -f "" ]; then
        echo "The file \"\" does not exist!"
        exit 1
    fi

    if [ ! -f "" ]; then
        echo "The file \"\" does not exist!"
        exit 1
    fi
fi

How can I do that?

我怎样才能做到这一点?

I want to know what the most logical (STANDARD) method is.

我想知道最合乎逻辑的(STANDARD)方法是什么。

回答by Chris Maes

Of course you can do that... . There are many ways to obtain this. The most simple maybe:

你当然可以这样做...... 有很多方法可以获得这个。最简单的也许是:

if [ ! -f "" ]; then
    NOT1=1
fi
if [ ! -f "" ]; then
    NOT2=1
fi
if [ -n "$NOT1" ] && [ -n "$NOT2" ] 
....

If you don't want to do the checks two time, you can work with variables; something like this:

如果你不想做两次检查,你可以使用变量;像这样:

status=""
for file; do
    [ -f "$file" ]
    status+=$?
done

case $status in
    11)
        echo "The files \"\" and \"\" do not exist!"
        exit 1
        ;;
    10)
        echo "The file \"\" does not exist!"
        exit 1
        ;;
    01)
        echo "The file \"\" does not exist!"
        exit 1
        ;;
esac

回答by John B

You could do it like this so you only have to testeach file once:

您可以这样做,这样您只需对test每个文件进行一次:

#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "Usage: ${0##*/} <File1> <File2>"
elif [[ ! -f  && ! -f  ]]; then
    echo "The files \"\" and \"\" don't exist!"
elif [[ ! -f  ]]; then
    echo "The file \"\" does not exist!"
elif [[ ! -f  ]]; then
    echo "The file \"\" does not exist!"
fi

回答by konsolebox

Simplified for Bash:

简化为Bash

if [ ! -f "" ]; then
    if [ ! -f "" ]; then
        echo "The files \"$file1\" and \"$file2\" don't exist!"
        exit 1
    else
        echo "The file \"\" does not exist!"
        exit 1
    fi
fi

if [ ! -f "" ]; then
    echo "The file \"\" does not exist!"
    exit 1
fi

回答by user3132194

The logical is

逻辑是

if [ ! -f "" -a ! -f "" ]; then
    echo "The files \"$file1\" and \"$file2\" don't exist!"
    exit 1
fi

if [ ! -f "" ]; then
    echo "The file \"\" does not exist!"
    exit 1
fi

if [ ! -f "" ]; then
    echo "The file \"\" does not exist!"
    exit 1
fi

The readable is

可读性是

if [ ! -f "" ] && [ ! -f "" ]; then
  echo "Both files does not exist!"
  exit 1
fi

The readable is preferred.

可读性强者优先。

Also not testing for existence twice as Chris Maes said could be logical too.

也没有像 Chris Maes 所说的那样对存在进行两次测试也可能是合乎逻辑的。

回答by kamituel

You can use this additional condition:

您可以使用此附加条件:

if [ ! -f "" ] || [ ! -f "" ]; then
  USAGE
  exit 1
fi

I'm not sure you need that though. The two conditions you have, for first and second file, will be informative enough.

我不确定你需要那个。对于第一个和第二个文件,您拥有的两个条件将提供足够的信息。

Alternatively, you can use just one condition by using logical OR ||. I'd suspect users wouldn't have an issue with understanding their misues of the script:

或者,您可以通过使用逻辑 OR 仅使用一种条件||。我怀疑用户在理解他们对脚本的错误方面不会有问题:

##代码##