如何检查 Bash 中是否存在某些文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/572206/
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
How do I check if certain files exist in Bash?
提问by Frank
In a bashscript, I have to check for the existence of several files.
在bash脚本中,我必须检查是否存在多个文件。
I know an awkward way to do it, which is as follows, but that would mean that my main program has to be within that ugly nested structure:
我知道一种笨拙的方法,如下所示,但这意味着我的主程序必须在那个丑陋的嵌套结构中:
if [ -f $FILE1 ]
then
if [ -f $FILE2 ]
then
echo OK
# MAIN PROGRAM HERE
fi
fi
The following version does not work:
以下版本不起作用:
([ -f $FILE1 ] && [ -f $FILE2 ]) || ( echo "NOT FOUND"; exit 1 )
echo OK
It prints
它打印
NOT FOUND
OK
Is there an elegant way to do this right?
有没有一种优雅的方法来做到这一点?
UPDATE:See the accepted answer. In addition, in terms of elegance I like Jonathan Leffler's answer:
更新:请参阅已接受的答案。此外,在优雅方面,我喜欢Jonathan Leffler 的回答:
arg0=$(basename if [[ ! ( -f $FILE1 && -f $FILE2 ) ]]; then
echo NOT FOUND
exit 1
fi
# do stuff
echo OK
.sh)
error()
{
echo "$arg0: $@" 1>&2
exit 1
}
[ -f $FILE2 ] || error "$FILE2 not found"
[ -f $FILE1 ] || error "$FILE1 not found"
回答by Johannes Schaub - litb
How about
怎么样
[ -f "$FILE1" ] && [ -f "$FILE2" ] || { echo "NOT FOUND"; exit 1; }
See help [[and help testfor the options usable with the [[style tests. Also read this faq entry.
查看help [[和help test可用于[[样式测试的选项。另请阅读此常见问题条目。
Your version does not work because (...)spawns a new sub-shell, in which the exitis executed. It therefor only affects that subshell, but not the executing script.
您的版本不起作用,因为(...)产生了一个新的子外壳,在其中exit执行 。因此它只影响那个子shell,而不影响正在执行的脚本。
The following works instead, executing the commands between {...}in the current shell.
下面的工作代替,{...}在当前 shell 中执行命令。
I should also note that you have to quote both variables to ensure there is no unwanted expansion or word splitting made (they have to be passed as one argument to [).
我还应该注意,您必须引用这两个变量以确保不会进行不必要的扩展或分词(它们必须作为一个参数传递给[)。
if [ -f $FILE1 -a -f $FILE2 ]; then
echo OK
fi
回答by Greg Hewgill
I think you're looking for:
我认为您正在寻找:
file_list='file1 file2 wild*'
for file in $file_list; do
[ -f $file ] || exit
done
do_main_stuff
See man testfor more details on what you can put inside the [ ].
有关man test可以放入[ ].
回答by Adam Liss
You can list the files and check them in a loop:
您可以列出文件并循环检查它们:
arg0=$(basename ##代码## .sh)
error()
{
echo "$arg0: $@" 1>&2
exit 1
}
[ -f $FILE2 ] || error "$FILE2 not found"
[ -f $FILE1 ] || error "$FILE1 not found"
回答by Jonathan Leffler
I usually use a variant on:
我通常在以下方面使用变体:
##代码##There's no particular virtue in making the shell script have a single exit point - no harm either, but fatal errors may as well terminate the script.
使 shell 脚本有一个单一的退出点并没有什么特别的好处——也没有坏处,但致命错误也可能会终止脚本。
The only point of debate would be whether to diagnose as many problems as possible before exiting, or whether to just diagnose the first. On average, diagnosing just the first is a whole lot easier.
唯一的争论点是在退出之前是否诊断出尽可能多的问题,还是只诊断第一个。平均而言,仅诊断第一个要容易得多。

