想要将参数传递给我的shell函数

时间:2013-07-27 08:26:51

标签: bash shell

我对bash / shell函数感到非常沮丧。

我有这个命令:

grep -r '^struct task_struct ' include

我想在我的bashrc中添加一个函数或别名,让我说出类似

的内容
grepdefined "struct task_struct" 

让它从上面运行命令。然后我可以用“struct task_info”或其他东西运行它。太令人沮丧了。

我现在有了这个,因为我测试了所有内容并猜测了问题:

function grepdefined() {
    test="$@";
    echo $test;
    grep -rni '^'$test' ' include;
    #echo "grep -rni'^'$test' ' include;"
}

它只搜索“struct task_struct”中的第一个单词“struct”,我正在使用grepdefined“struct task_struct”传递它。

1 个答案:

答案 0 :(得分:4)

像这样修改你的grep行:

grep -rni "^$test"  include

<强>更新

grep -rni "^$test"  include

实际上变成了:

grep -rni "^struct task_struct"  include

然而,

grep -rni '^'$test' ' include

被解释为:

grep -rni ^struct task_struct include

如果您注意到上述情况,则grep的搜索模式仅为^struct,而task_struct被视为与include一起搜索模式的文件之一,因此问题你面对的只是struct而不是整个事情。每当它是一个多措辞的字符串时,它必须加倍引用。