在bashrc中保存公共Grep模式
我经常使用符合以下模式的grep在bashrc中保存公共Grep模式,bash,grep,Bash,Grep,我经常使用符合以下模式的grep grep -irs "main" . --exclude-dir={env,build,release-info} --include \*.java 我试图找到是否可以将这个模式保存到我的bashrc中,这样我就不必每次都输入巨大的grep行 对于bashc中的grep别名,我希望将以下内容作为参数传递 搜索字符串 ——包括模式。我可以将*.java作为默认值吗?如果传递了不同的值,则覆盖默认值 类似地,我可以让--exclude dir
grep -irs "main" . --exclude-dir={env,build,release-info} --include \*.java
我试图找到是否可以将这个模式保存到我的bashrc中,这样我就不必每次都输入巨大的grep行
对于bashc中的grep别名,我希望将以下内容作为参数传递
——包括
模式。我可以将*.java
作为默认值吗?如果传递了不同的值,则覆盖默认值--exclude dir
具有默认值。如果传递了一个值,则覆盖默认值我怎样才能做到这一点?谢谢您的建议。按照建议,使用shell函数。确切的功能取决于您想要的接口类型 例如:
grepmain(){
if [ "$excldir" = "" ] ; then
grepmainexcl="env,build,release-info"
else
grepmainexcl="$excldir"
fi
grep -irs "main" . "--exclude-dir={$grepmainexcl}" --include "$@"
}
并将其用作:
grepmain \*.java
excldir=env,build grepmain \*.java
您还可以在函数的excludedir中使用类似
-e
的标志;如果其他人会使用它,您可能还希望在函数中包含一些有效性检查。如建议的,使用shell函数。确切的功能取决于您想要的接口类型
例如:
grepmain(){
if [ "$excldir" = "" ] ; then
grepmainexcl="env,build,release-info"
else
grepmainexcl="$excldir"
fi
grep -irs "main" . "--exclude-dir={$grepmainexcl}" --include "$@"
}
并将其用作:
grepmain \*.java
excldir=env,build grepmain \*.java
您还可以在函数的excludedir中使用类似
-e
的标志;如果其他人会使用它,您可能也希望在函数中包含一些有效性检查。您的.bashrc中的函数可能看起来非常简单:function mygrep {
s=${1:-"main"}
e=${2:-"env,build,release-info"}
i=${3:-"\*.java"}
grep -irs "$s" . "--exclude-dir={$e}" --include "$i"
}
赋值是定义变量默认值的标准方法
下面是一些使用示例(为了调试,我在.bashrc中的grep语句周围放了一个set-x
).bashrc中的函数看起来很简单:
function mygrep {
s=${1:-"main"}
e=${2:-"env,build,release-info"}
i=${3:-"\*.java"}
grep -irs "$s" . "--exclude-dir={$e}" --include "$i"
}
赋值是定义变量默认值的标准方法
下面是一些使用示例(为了调试,我在.bashrc中的grep语句周围放了一个set-x
)当然,最简单的方法是定义。我不确定您希望参数如何工作。使用Bash比使用真正的编程语言更烦人,但您肯定可以让它运行起来。我正试图通过Bash实现这一点。Shell函数似乎是更好的选择。谢谢你的建议!Shell函数是
bash
:)的一项功能。我将您链接到适当的文档。当然,最简单的方法是定义。我不确定您希望参数如何工作。使用Bash比使用真正的编程语言更烦人,但您肯定可以让它运行起来。我正试图通过Bash实现这一点。Shell函数似乎是更好的选择。谢谢你的建议!Shell函数是bash
:)的一项功能。我将您链接到适当的文档。