以编程方式将 glob 添加到 bash 命令
Programmatically add glob to bash command
如果我的参数在 bash 脚本中不满足,我正在寻找一种扩展 glob 的方法,我不是肯定的,但我认为它可能需要 eval 或类似的东西,但是想不起来了。
函数
function search ()
{
[ 'x' == "x" ] && {
what="*"
} || {
what=""
}
grep -n -Iir "" "${what}"
}
没有 arg2 的预期结果
grep -n -Iir 'something' * ## ran as the normal command
请记住,*
在 grep
开始之前由 shell 扩展为文件名列表。因此,您可以自己扩展它们:
search() {
local tgt=; shift # move first argument into local variable tgt
(( "$#" )) || set -- * # if no other arguments exist, replace the remaining argument
# ...list with filenames in the current directory.
grep -n -Iir "$tgt" "$@" # pass full list of arguments through to grep
}
你有句法问题:你希望 </code> 被引用,但如果是 <code>*
则不需要。因此,您只需要两个命令:
search () {
if [ -z "" ]; then
grep -n -Iir "" *
else
grep -n -Iir "" ""
fi
}
如果我的参数在 bash 脚本中不满足,我正在寻找一种扩展 glob 的方法,我不是肯定的,但我认为它可能需要 eval 或类似的东西,但是想不起来了。
函数
function search ()
{
[ 'x' == "x" ] && {
what="*"
} || {
what=""
}
grep -n -Iir "" "${what}"
}
没有 arg2 的预期结果
grep -n -Iir 'something' * ## ran as the normal command
请记住,*
在 grep
开始之前由 shell 扩展为文件名列表。因此,您可以自己扩展它们:
search() {
local tgt=; shift # move first argument into local variable tgt
(( "$#" )) || set -- * # if no other arguments exist, replace the remaining argument
# ...list with filenames in the current directory.
grep -n -Iir "$tgt" "$@" # pass full list of arguments through to grep
}
你有句法问题:你希望 </code> 被引用,但如果是 <code>*
则不需要。因此,您只需要两个命令:
search () {
if [ -z "" ]; then
grep -n -Iir "" *
else
grep -n -Iir "" ""
fi
}