Bashのtcsh dextractに似たPushd機能を使用してください。

Bashのtcsh dextractに似たPushd機能を使用してください。

bash私は(4.1.2(1)-release)が好きですが、そのオプションを有効にする方法がとても良いので、デフォルトのシェルとしての使用を拒否します。ディレクトリスタックなどのコマンドセットを実装した人はいますか?あるいは、アクティベーションのように動作するように欺く方法が見つからなかったかもしれません。tcshpushd +Ndextractbashdextractbashbashtcshdextract

ポイントはbash次のpushd +Nとおりです。

$ cd /tmp
$ pushd a
/tmp/a /tmp
$ pushd ../b
/tmp/b /tmp/a /tmp
$ pushd ../c
/tmp/c /tmp/b /tmp/a /tmp
$ pushd +1
/tmp/b /tmp/a /tmp /tmp/c

を実行すると、他のすべてのディレクトリの場所がデフォルトbashで回転するのはなぜですか?これがうまくいくのはなぜですか? (たぶん誰かが説明すると、この動作に感謝し慣れているかもしれません。)ただ抽出して一番上に置くwithと比較すると、tcshpushd +1tcshdextract

% set dextract
% cd /tmp
% pushd a
/tmp/a /tmp 
% pushd ../b
/tmp/b /tmp/a /tmp 
% pushd ../c
/tmp/c /tmp/b /tmp/a /tmp 
% pushd +1
/tmp/b /tmp/c /tmp/a /tmp 

残りのディレクトリ順序は変更されません。注文が循環しないときに頭の中で追跡する方が簡単なので、常に必要なdirsカタログを実行して検索する必要がないため、これが重要だと思います。

一度試してみたら、どこから始めるべきですか?変数があることを確認しましたがDIRSTACK、変数を変更するとスタックが変更されますが(たとえ不正確ですが)正しくありません(スタックに項目が4つあれば空です)。

ベストアンサー1

少しの仕事とインスピレーションを得た後、問題を解決しました。

# dextract-like pushd -- push $1'st directory to top of stack and shift rest down
function pushd {
    local t
    if [[ $1 =~ ^[-+]?[0-9]+$ ]]; then
        pos=$1
        [[ ${pos} -lt 0 ]] && ((pos=${#DIRSTACK[@]}+${pos}))
        if [[ ${pos} -gt 0 && ${pos} -lt ${#DIRSTACK[@]} ]]; then
            t="${DIRSTACK[$pos]}"
            # This is crufty. You can't put ~ in DIRSTACK, except the
            # shell will put it into position zero when it refers to your
            # own directory (not anyone else's!). So replace any occurrences.
            DIRSTACK=("" "${DIRSTACK[0]/#~/${HOME}}" "${DIRSTACK[@]:1:${pos}-1}" "${DIRSTACK[@]:${pos}+1}")
            cd "$t"
            builtin dirs -v
        else
            echo "$0: pushd: $1: directory stack index out of range" 1>&2
        fi
    else
        builtin pushd "$@" >/dev/null || return
        cd .
        builtin dirs -v
    fi
}

おすすめ記事