2009-08-06 24 views
0

私は/etc/bash_completionスクリプトをいくつかのDebianパッケージにあります。私は、特定のディレクトリ(デフォルトで/etc/bash_completion.d/)を見て、そのディレクトリ内のすべてのファイルを参照するコードを使用することに興味がありました。Bashの文字列比較の構文

残念ながら、スクリプトを実行しようとすると、Mac OS X版のbashでエラーが発生します。問題の行は、次のとおりです。

for i in $BASH_COMPLETION_DIR/*; do 
    [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|.rpm*) ]] && 
    [ \(-f $i -o -h $i \) -a -r $i ] && . $i 
done 

具体的には、bash(3.2.17)の私のバージョンは@()建設にチョーク。最初のテストのポイントは、エディタのスワップファイルやバックアップなどのソースがないことを確認することです。具体的には@()の構文が何であるかを理解したいと思います。と同様にエレガントな)私の古代bashのコピーを実行しています。誰も洞察力を提供できますか?

答えて

3

これは、grep "または"演算子(|)に相当するシェル比較の単なる拡張です。

bashのバージョンによっては、利用できない場合があります。にshoptを組み込む必要があります。次のセッションのトランスクリプトを参照してください。

 
    [email protected]> $ bash --version 
     GNU bash, version 3.2.48(21)-release (i686-pc-cygwin) 
     Copyright (C) 2007 Free Software Foundation, Inc. 
    [email protected]> echo @(*~|*.pl) 
     bash: syntax error near unexpected token '(' 
    [email protected]> shopt extglob 
     extglob off 
    [email protected]> shopt -s extglob 
    [email protected]> echo @(*~|*.pl) 
     qq.pl qq.sh~ xx.pl 
    [email protected]> 

仕事に次のことをできること:

 
    ?(pattern-list) 
     Matches zero or one occurrence of the given patterns 
    *(pattern-list) 
     Matches zero or more occurrences of the given patterns 
    +(pattern-list) 
     Matches one or more occurrences of the given patterns 
    @(pattern-list) 
     Matches one of the given patterns 
    !(pattern-list) 
     Matches anything except one of the given patterns 

あなたはそれがshopt作業を得ることができない場合は、そのような古い方法と同様の効果を生成することができます次のようになります。

 
    #!/bin/bash 
    for i in $BASH_COMPLETION_DIR/*; do 
     # Ignore VIM, backup, swp, files with all #'s and install package files. 
     # I think that's the right meaning for the '\#*\#' string. 
     # I don't know for sure what it's meant to match otherwise. 

     echo $i | egrep '~$|\.bak$|\.swp$|^#*#$|\.dpkg|\.rpm' >/dev/null 2>&1 
     if [[ $? == 0 ]] ; then 
      . $i 
     fi 
    done 

また、複数の複雑な決定があり、それがソースになるかどうかを決定します最初にtrueに設定されているdoit変数を使用し、これらの条件のいずれかがトリガーされた場合はfalseに設定することができます。たとえば、次のスクリプトqq.sh:

 
    #!/bin/bash 
    for i in * ; do 
     doit=1 

     # Ignore VIM backups. 
     echo $i | egrep '~$' >/dev/null 2>&1 
     if [[ $? -eq 0 ]] ; then 
      doit=0 
     fi 

     # Ignore Perl files. 
     echo $i | egrep '\.pl$' >/dev/null 2>&1 
     if [[ $? -eq 0 ]] ; then 
      doit=0 
     fi 

     if [[ ${doit} -eq 1 ]] ; then 
      echo Processing $i 
     else 
      echo Ignoring $i 
     fi 
    done 

は私のホームディレクトリにこれをした:Bashのマニュアルの

 
    Processing Makefile 
    Processing binmath.c 
    : : : : : 
    Processing qq.in 
    Ignoring qq.pl --+ 
    Processing qq.sh | 
    Ignoring qq.sh~ --+--- see? 
    Processing qqin  | 
    : : : : :   | 
    Ignoring xx.pl --+ 
+1

関連ページ:http://www.gnu.org/software/bashを/manual/html_node/Pattern-Matching.htmlとhttp://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html –

+0

アダムに感謝、私はそれらを探していました。 – Jim

+0

優れています。 shoptのことがうまくいった!説明をありがとう。 –