2011-08-28 23 views
19

私はcdargsパッケージで "cdargs-bash.sh"スクリプトを理解しようとしています。bash変数の改行文字を置き換えますか?

function _cdargs_get_dir() 
{ 
local bookmark extrapath 
# if there is one exact match (possibly with extra path info after it), 
# then just use that match without calling cdargs 
if [ -e "$HOME/.cdargs" ]; then 
    dir=`/bin/grep "^$1 " "$HOME/.cdargs"` 
    if [ -z "$dir" ]; then 
     bookmark="${1/\/*/}" 
     if [ "$bookmark" != "$1" ]; then 
      dir=`/bin/grep "^$bookmark " "$HOME/.cdargs"` 
      extrapath=`echo "$1" | /bin/sed 's#^[^/]*/#/#'` 
     fi 
    fi 
    [ -n "$dir" ] && dir=`echo "$dir" | /bin/sed 's/^[^ ]* //'` 
fi 
if [ -z "$dir" -o "$dir" != "${dir/ 
/}" ]; then 
    # okay, we need cdargs to resolve this one. 
    # note: intentionally retain any extra path to add back to selection. 
    dir= 
    if cdargs --noresolve "${1/\/*/}"; then 
     dir=`cat "$HOME/.cdargsresult"` 
     /bin/rm -f "$HOME/.cdargsresult"; 
    fi 
fi 
if [ -z "$dir" ]; then 
    echo "Aborted: no directory selected" >&2 
    return 1 
fi 
[ -n "$extrapath" ] && dir="$dir$extrapath" 
if [ ! -d "$dir" ]; then 
    echo "Failed: no such directory '$dir'" >&2 
    return 2 
fi 

}

テストの目的は何:そして、私は次の関数で疑問持っ

"$dir" != "${dir/ 
/}" 

ここでは、2つの回線でのテストスパンを。 $dirの改行文字を削除したいのですか、それとも別の理由で削除したいですか?私はちょうどbashのスクリプティングを学び始めているし、私はグーグルではあるが、このような使用法は見つけられなかった。

答えて

35

はい、あなたは正しいです、改行文字を削除します。私は、テストの目的は、$dirに複数の行が含まれていないことを確認することだと思います。

また、私はそれが良く見えると思うので、これは2つの行を必要としない

${dir/$'\n'/} 

\newlineを削除することができます。

+0

あなたの提案はうまくいきます。 '$ '\ n"はここでの変数置換ですか?私はそれを理解できませんでした。 – yorua007

+4

いいえ、ANSI-C引用と呼ばれています。http://www.gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting –

+0

を参照してください。どうもありがとう。 – yorua007

関連する問題