bash 检查是否存在多个文件——使用参数的 Unix 脚本

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

Checking if multiple files exist — Unix script using arguments

bashunix

提问by user2836537

I'm trying to write a UNIX shell script that will take a few file names as arguments and then search in the current directory for these files.

我正在尝试编写一个 UNIX shell 脚本,它将一些文件名作为参数,然后在当前目录中搜索这些文件。

So I would type

所以我会输入

$ script file1 file2 file3...

$脚本文件1文件2文件3...

If the file name(s) exist it would exit with a 1; however if the file(s) do not exist it will end with a status 0.

如果文件名存在,它将以 1 退出;但是,如果文件不存在,它将以状态 0 结束。

Here is what I have so far:

这是我到目前为止所拥有的:

#!/bin/bash
FILE=

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

but it's for only one file at a time passed as an argument. I need multiple files at one time as arguments.

但它一次仅用于一个文件作为参数传递。我一次需要多个文件作为参数。

回答by Jonathan Leffler

The question is incomplete — do you want exit status 1:

问题不完整——你想要退出状态 1:

  • if every one of the files is present,
  • if every one of the files is missing,
  • if at least one of the files is present,
  • if at least one of the files is missing,
  • or do you have some other N of M criterion in mind?
  • 如果每个文件都存在,
  • 如果每个文件都丢失,
  • 如果存在至少一个文件,
  • 如果至少有一个文件丢失,
  • 或者你有其他一些 N of M 标准吗?

The fundamental technique for iterating over file name arguments is:

迭代文件名参数的基本技术是:

for file in "$@"
do
    ...body of loop...using "$file" when referring to the file
done

See also How to iterate over the arguments in a bashscript.

另请参阅如何迭代bash脚本中的参数



If you want exit status 1 when every one of the files is present, you can exit with status 0 when you detect that a file is missing:

如果您希望在每个文件都存在时退出状态 1,则可以在检测到文件丢失时以状态 0 退出:

for file in "$@"
do
    if [ ! -e "$file" ]
    then echo "$file is missing" >&2; exit 0
    fi
done
exit 1

If you want exit status 1 when every one of the files is missing, you can exit with status 0 when you detect that a file is present:

如果您希望在每个文件都丢失时退出状态 1,则可以在检测到文件存在时以状态 0 退出:

for file in "$@"
do
    if [ -e "$file" ]
    then echo "$file is present" >&2; exit 0
    fi
done
exit 1

If you want exit status 1 when at least one of the files is present, you can exit with status 1 when you detect that a file is present:

如果您希望在至少存在一个文件时退出状态 1,则可以在检测到文件存在时以状态 1 退出:

for file in "$@"
do
    if [ -e "$file" ]
    then echo "$file is present" >&2; exit 1
    fi
done
exit 0

If you want exit status 1 when at least one of the files is missing, you can exit with status 1 when you detect that a file is missing:

如果您希望在至少一个文件丢失时退出状态 1,则可以在检测到文件丢失时退出状态 1:

for file in "$@"
do
    if [ ! -e "$file" ]
    then echo "$file is missing" >&2; exit 1
    fi
done
exit 0

If you have some other criterion in mind, you need to explain what it is, and how it would be applied for the cases 0, 1, 2, ... M files are specified on the command line.

如果您有其他一些标准,您需要解释它是什么,以及如何将其应用于命令行上指定的 0、1、2、... M 文件。

If you want to see a report on each file, you need to use a slightly different structure. For the last case, where you want exit status 1 if at least one of the files is missing, you set the variable statusto 0 before the loop, and in the body of the loop, set it to 1 if the condition is met, finally exiting with the current value of status.

如果要查看每个文件的报告,则需要使用略有不同的结构。对于最后一种情况,如果至少缺少一个文件,您希望退出状态为 1,则在status循环之前将变量设置为 0,在循环体中,如果满足条件,则将其设置为 1,最后以当前状态值退出。

status=0
for file in "$@"
do
    if [ ! -e "$file" ]
    then echo "$file is missing" >&2; status=1
    fi
done
exit $status

The changes for the other cases are similar; set the initial status to the default value, and override the default when you detect the contrary condition.

其他情况的变化类似;将初始状态设置为默认值,并在检测到相反条件时覆盖默认值。

I used -eto test whether the name exists; you can use other more specific tests if the name must be a plain file, or a directory, or a block or character special device, or a symlink, or a FIFO or a socket or …

我曾经-e测试过这个名字是否存在;如果名称必须是纯文件、目录、块或字符特殊设备、符号链接、FIFO 或套接字或……

I used the >&2notation so that the reports are sent to standard error rather than standard output. You might prefer to prefix the messages with $0, the name of the script. You might prefer that the messages are reported on standard output.

我使用了这个>&2符号,以便将报告发送到标准错误而不是标准输出。您可能更喜欢在消息前加上$0, 脚本名称。您可能更喜欢在标准输出上报告消息。