bash 脚本中的通配符扩展

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

wildcard expansion in bash script

bash

提问by Morse

if I write this string in my script:

如果我在我的脚本中写这个字符串:

list=$(ls /path/to/some/files/*/*/*.dat)

it works fine. But what I need is

它工作正常。但我需要的是

files="files/*/*/*.dat"
list=$(ls /path/to/some/${files})

and it says

它说

ls: /path/to/some/files/*/*/*.dat: No such file or directory

How should I do it?

我该怎么做?

采纳答案by kev

Try this:

尝试这个:

list=$(find /path/to/some/files/ -mindepth 3 -maxdepth 3 -name '*.dat')

回答by chepner

If you only get that message where there truly are no matching .datfiles, add this to your script:

如果您只在确实没有匹配.dat文件的情况下收到该消息,请将其添加到您的脚本中:

shopt -s nullglob

It will cause the glob to expand to an empty list if there are no matching files, rather than being treated literally.

如果没有匹配的文件,它会导致 glob 扩展为空列表,而不是按字面意思处理。