将参数传递给bash函数会导致错误

时间:2015-11-18 16:56:54

标签: bash shell

我试图将命令行上的参数传递给〜/ .bash_profile中定义的函数。我已经在stackoverflow上查看了许多解决方案,但它们并没有工作。基本上,格式是:

function rgrep  { /usr/bin/grep -rl '$@' . | /usr/bin/grep -v 'node_modules' | /usr/bin/grep -v 'bower'; }

然而,当我尝试这个时:

rgrep 'foo'

我得到了

grep: foo: No such file or directory

1 个答案:

答案 0 :(得分:1)

通常情况下,如果从命令行获得命令序列“work”,那么它可以作为“函数”工作 - 从命令行执行命令'work'吗?

另外,为避免可能与现有命令混淆,我建议添加'前缀',即'tf_'#test function。

你需要双引号 - (“)而不是(')...... 我建议尝试更简单的事情:

function tf_rgrep  { grep -rl "$@" . | grep -ve "node_modules|bower" ;}  

要进行调试,请添加'set -x':

function tf_rgrep  { set -x; grep -rl "$@" . | grep -ve "node_modules|bower" ; set - ;}   

## try 'egrep' if above not working

function tf_rgrep {set -x; egrep -rl“$ @”。 | egrep -ve“node_modules | bower”; set - ;}

将上述内容保存到文件中,即/tmp/test.functions.sh
然后使用'source'来测试......

source /tmp/test.functions.sh#编辑并重复直到工作

准备好/工作后,添加到您的个人资料中。

:)
戴尔