Chinaunix

标题: grep 问题,做了一个grep 的别名 grepch='grep --include=*.[chCH] -rn --file=./' [打印本页]

作者: reer    时间: 2011-12-12 17:47
标题: grep 问题,做了一个grep 的别名 grepch='grep --include=*.[chCH] -rn --file=./'
想实现这样的用法:
grepch PATTERN

也就是不用在PATTERN 后面加上FILE,用下面的别名不能实现目的:
alias grepch='grep --include=*.[chCH] -rn --file=./'

请教正确的做法是怎样呢?
作者: Shell_HAT    时间: 2011-12-12 20:13
不加FILE,你让它搜索哪个文件?
作者: reer    时间: 2011-12-12 22:34
我是想搜索当前目录所有文件和子目录
作者: chenyx    时间: 2011-12-13 09:09
本帖最后由 chenyx 于 2011-12-13 14:53 编辑

楼主尝试写一个函数,放到~/.bash_profile里面
  1. grepch () {
  2. /bin/grep $1 -rn *
  3. }
复制代码
我的比较简单,你根据实际情况,自己修改下
作者: chenyx    时间: 2011-12-13 09:11
或者,在/bin下写一个grepch的脚本,赋予grepch脚本x权限,应该也可以实现你的需求
作者: reer    时间: 2011-12-13 14:45
本帖最后由 reer 于 2011-12-13 14:46 编辑

首先谢谢chenyx 啊!

在 ~/.bash_profile 里加了上上个帖子里的内容后,source ~/.bash_profile 的时候提示报错:
bash: /home/pax/.bash_profile: line 19: syntax error near unexpected token `('
bash: /home/pax/.bash_profile: line 19: `grepch()'
作者: chenyx    时间: 2011-12-13 14:48
sorry,写错了,应该缺个空格,帖子我编辑了,你试试修改下
作者: reer    时间: 2011-12-13 14:57
sorry,chenyx ,这个报错不是空格的原因,原来是我在 /etc/bashrc里设置过:
alias grepch='grep --include=*.[chCH] -rn'

所以在 ~/.bash_profile 里再设置同样名字的 grepch 的时候才报这个错。

我把名字改一下,改成gch就可以了,没有gch后面没有空格也不会错了:
gch()
{
/bin/grep $1 -rn *
}

再次感谢 chenyx!
作者: chenyx    时间: 2011-12-13 15:00
回复 8# reer


    哦,原来你已经在profile里面定义过alias了.好用就行.我的那个还比较粗糙,你自己修改下吧,比如添加判断$1是否存在,等
作者: reer    时间: 2011-12-13 15:04
对了,我最后还是加到 /etc/bashrc里去了,这样所有的用户都可以用这个命令
作者: reer    时间: 2011-12-13 15:06
回复 9# chenyx

不用修改了,这样就是我想要的功能了
作者: chenyx    时间: 2011-12-13 15:12
回复 11# reer


    修改还是有用的,比如你想添加功能,传递多余的参数,等等.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2