Bash 要将参数传递给我的shell函数吗
我对bash/shell函数非常失望 我有这个命令:Bash 要将参数传递给我的shell函数吗,bash,shell,Bash,Shell,我对bash/shell函数非常失望 我有这个命令: grep -r '^struct task_struct ' include 我想给我的bashrc添加一个函数或别名,这样我就可以说 grepdefined "struct task_struct" 让它从上面运行命令。还有一次,我可以用“struct task_info”之类的东西来运行它。太令人沮丧了 当我测试所有东西并猜测问题时,我现在有了这个问题: function grepdefined() { test="$@";
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\u struct”中的第一个单词“struct”,我用grepdefined“struct task\u struct”传递它。修改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\u struct
与include
一起被视为搜索模式的文件之一,因此您面临的问题是,它只考虑struct
而不是整个问题。如果是多字字符串,则必须使用双引号。修改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\u struct
与include
一起被视为搜索模式的文件之一,因此您面临的问题是,它只考虑struct
而不是整个问题。如果是多字字符串,则必须使用双引号。非常感谢。你能告诉我为什么会这样吗?我知道它必须是带有转义字符串的东西,但我还远远不能理解它。@cdietschrun:用更多注释更新了解决方案。谢谢。你能告诉我为什么会这样吗?我知道它必须是带有转义字符串的东西,但我远远不能理解它。@cdietschrun:用更多的注释更新了解决方案