Bash 函数不递归工作

far*_*ang 0 bash find shell-script function wildcards

目录结构:

一.pdf
./subdir/two.pdf
./anothersubdir/三.pdf

当我输入:

find ./ -type f -name "*.pdf"    
Run Code Online (Sandbox Code Playgroud)

它检索所有 pdf,包括子目录。

bash 函数

function getext {find ./ -type f -name "$1"}
Run Code Online (Sandbox Code Playgroud)

在 bashrc 中使用此函数,输入:

getext *.pdf
Run Code Online (Sandbox Code Playgroud)

它只检索“one.pdf”,但不检索其余部分。

问题:这个函数会发生什么?与仅获取第一个文件并停止的标准输入相比,它缺少什么?

感谢您的帮助。

ste*_*ver 5

出于与在函数内部"*.pdf"引用参数的相同原因,您需要在调用函数时引用它:find

getext "*.pdf"
Run Code Online (Sandbox Code Playgroud)

否则,shell 将尝试匹配*.pdf当前目录中的文件名,导致文件在传递给函数之前one.pdf被扩展(在本例中为扩展) 。