0

複数の引数を指定して、bash エイリアスと bash 関数の両方を使用したいと考えています。svn サブコマンドをエミュレートします。

$ svngrep -nr 'Foo' .
$ svn grep -nr 'Foo' .

私の期待は、両方が以下のように機能することです。

grep --exclude='*.svn-*' --exclude='entries' -nr 'Foo' .

しかし実際には、エイリアス ('svngrep') だけがうまく機能し、関数 ('svn grep') は無効なオプション エラーを引き起こします。.bashrc の書き方は?

#~/.bashrc

alias svngrep="grep --exclude='*.svn-*' --exclude='entries'"

svn() {
  if [[ $1 == grep ]]
then
  local remains=$(echo $@ | sed -e 's/grep//')
  command "$svngrep $remains"
else
  command svn "$@"
fi
}
4

3 に答える 3

2

shift位置パラメーターから最初の単語を削除したい場合: これにより、 の配列のような性質が維持されます"$@"

svn() {
  if [[ $1 = grep ]]; then
    shift
    svngrep "$@"
  else
    command svn "$@"
  fi
}

bash の[[ビルトインでは、single=は文字列の等価性に使用され、double==はパターン マッチングに使用されます。この場合は前者のみが必要です。

于 2012-06-09T10:35:56.673 に答える
0

svngrep変数ではありません。これは bash で使用されるエイリアスです。したがって、次のような新しい変数を作成する必要があります。

svngrep_var="grep --exclude='*.svn-*' --exclude='entries'"

そしてそれをスニペットで使用します:

...
command "$svngrep_var $remains"
...
于 2012-06-09T07:49:27.240 に答える
0

これを自分でリファクタリングします。そして元気に働きましょう!ありがとう!

#~/.bashrc
alias svngrep="svn grep"
svn() {
if [[ $1 == grep ]]
then
    local remains=$(echo $* | sed -e 's/grep//')
    command grep --exclude='*.svn-*' --exclude='entries' $remains
else
  command svn $*
fi
}

エイリアスをシンプルに保つことを選択しました。また、$@ の代わりに $* を使用します。

編集日: 2012-06-11

#~/.bashrc
alias svngrep="svn grep"
svn() {
  if [[ $1 = grep ]]
  then
    shift
    command grep --exclude='*.svn-*' --exclude='entries' "$@"
  else
    command svn "$@"
  fi
}
于 2012-06-09T09:06:58.190 に答える