1

在不输入排除模式的情况下,如何让 grep 排除 ack 排除的文件(临时文件和版本控制目录)?

如此处所述,您可以使用 GREP_OPTIONS,但随后会遇到问题:使用 grep 而不清除 GREP_OPTIONS 的脚本将会中断。

更好的方法是什么?别名?或者另一个包装 grep 的脚本?

虽然显而易见的答案是alias grep="ack -a",但我在学术上对如何在不使用 ack 的情况下解决这个问题很感兴趣。

4

1 回答 1

0

我当前的解决方案是使用别名将智能选项的使用限制为我的交互式输入(而不是其他人的脚本)。这是我的 bash_aliases 中与 grep 相关的片段:

SMART_GREP_OPTIONS=

# Ensure colors are supported.
if [ -x /usr/bin/dircolors ]; then
    SMART_GREP_OPTIONS=' --color=auto'
fi


# Make grep more like ack.
#   Ignore binary files.
SMART_GREP_OPTIONS="$SMART_GREP_OPTIONS --binary-files=without-match"
# Check for exclude-dir to prevent invalid options in older versions of grep.
# Assume if we have exclude-dir, that we have exclude.
if /bin/grep --help | /bin/grep -- --exclude-dir &>/dev/null; then
    # Ignore version control folders.
    for PATTERN in .cvs .git .hg .svn; do
        SMART_GREP_OPTIONS="$SMART_GREP_OPTIONS --exclude-dir=$PATTERN"
    done
    # Ignore temp files.
    for PATTERN in *.swp; do
        SMART_GREP_OPTIONS="$SMART_GREP_OPTIONS --exclude=$PATTERN"
    done
fi
# This alias may override ghostscript. That's not a problem for me.
alias gs='grep $SMART_GREP_OPTIONS'

I do most of my grepping from vim and use vim-searchsavvy to setup similar options (I don't use my alias from vim).

于 2014-08-02T19:35:13.207 回答