Linux 如何将通配符参数传递给 bash 文件

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

How do I pass a wildcard parameter to a bash file

linuxbashparameterswildcard

提问by eisaacson

I'm trying to write a bash script that allows the user to pass a directory path using wildcards.

我正在尝试编写一个 bash 脚本,允许用户使用通配符传递目录路径。

For example,

例如,

bash show_files.sh *

when executed within this directory

在此目录中执行时

drw-r--r--  2 root root  4.0K Sep 18 11:33 dir_a
-rw-r--r--  1 root root   223 Sep 18 11:33 file_b.txt
-rw-rw-r--  1 root root   106 Oct 18 15:48 file_c.sql

would output:

会输出:

dir_a
file_b.txt
file_c.sql

The way it is right now, it outputs:

现在的方式,它输出:

dir_a

contents of show_files.sh:

内容show_files.sh

#!/bin/bash

dirs=""

for dir in $dirs
do
    echo $dir
done

采纳答案by Jonathan Leffler

The parent shell, the one invoking bash show_files.sh *, expands the *for you.

父外壳,即调用 的bash show_files.sh *那个,会*为您扩展。

In your script, you need to use:

在您的脚本中,您需要使用:

for dir in "$@"
do
    echo "$dir"
done

The double quotes ensure that multiple spaces etc in file names are handled correctly.

双引号确保正确处理文件名中的多个空格等。

See also How to iterate over arguments in a bashshell script.

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



Potentially confusing addendum

可能令人困惑的附录

If you're truly sure you want to get the script to expand the *, you have to make sure that *is passed to the script (enclosed in quotes, as in the other answers), and then make sure it is expanded at the right point in the processing (which is not trivial). At that point, I'd use an array.

如果您真的确定要让脚本扩展*,则必须确保将*其传递给脚本(用引号括起来,如其他答案中一样),然后确保它在正确的点展开在处理中(这不是微不足道的)。那时,我会使用一个数组。

names=( $@ )
for file in "${names[@]}"
do
    echo "$file"
done

I don't often use $@without the double quotes, but this is one time when it is more or less the correct thing to do. The tricky part is that it won't handle wild cards with spaces in very well.

我不经常在$@没有双引号的情况下使用,但这是一次或多或少正确的做法。棘手的部分是它不能很好地处理带有空格的通配符。

Consider:

考虑:

$ > "double  space.c"
$ > "double  space.h"
$ echo double\ \ space.?
double  space.c double  space.h
$

That works fine. But try passing that as a wild-card to the script and ... well, let's just say it gets to be tricky at that point.

这很好用。但是尝试将它作为通配符传递给脚本,然后......好吧,我们只能说到那时它会变得很棘手。

If you want to extract $2separately, then you can use:

如果你想$2单独提取,那么你可以使用:

names=(  )
for file in "${names[@]}"
do
    echo "$file"
done
# ... use  ...

回答by chepner

Quote the wild-card:

引用通配符:

bash show_files.sh '*'

or make your script accept a list of arguments, not just one:

或者让你的脚本接受一系列参数,而不仅仅是一个:

for dir in "$@"
do
    echo "$dir"
done

It's better to iterate directly over "$@'rather than assigning it to another variable, in order to preserve its special ability to hold elements that themselves contain whitespace.

最好直接迭代"$@'而不是将其分配给另一个变量,以保留其保存本身包含空格的元素的特殊能力。