测试来自给定模式的文件是否存在于 bash 中

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

test if file(s) from a given pattern exists in bash

bashscripting

提问by RSFalcon7

How do I test if a file (or many) named from given pattern exists and do stuff based on that test?

如何测试从给定模式命名的文件(或多个)是否存在并根据该测试执行操作?

This is my failed attempt:

这是我失败的尝试:

[[ { ls ../outputListWorkerPid_* |  wc -l } -ge "1" ]] && echo "ARRRRRR" || && echo "FAIL"

But I got the following error:

但我收到以下错误:

bash: conditional binary operator expected
bash: syntax error near `ls'

Also it would be nice to avoid the No such file or directoryfrom lsin case the doesn't exists.

如果No such file or directoryfromls不存在,也可以避免使用from 。

回答by Matt W

Use an if-statement:

使用 if 语句:

if [ "$(ls *.txt | wc -l)" -ge "1" ];
then
  echo "Yes";
else
  echo "No";
fi;

Does that answer your question?

这是否回答你的问题?