2009-06-12 22 views
8

KSHのコマンドにbash pushd/popdビルドと同等のものがありますか?やるbashで何のpushdとPOPD知らない人のために kshのpushd/popd?

は、ここで私は通常、この種のもののためのサブシェルを使用

pushd [-n] [dir] 
    pushd [-n] [+n] [-n] 
      Adds a directory to the top of the directory stack, or rotates 
      the stack, making the new top of the stack the current working 
      directory. With no arguments, exchanges the top two directo- 
      ries and returns 0, unless the directory stack is empty. 


    popd [-n] [+n] [-n] 
      Removes entries from the directory stack. With no arguments, 
      removes the top directory from the stack, and performs a cd to 
      the new top directory. Arguments, if supplied, have the fol- 
      lowing meanings: 

おかげ

+0

優れた 'pushd' /' popd'インストールがここにあります(http://blogs.oracle.com/nico/entry/ksh_functions_gal鉱石)は、おそらくSolarisでの使用に関連しています。 – Mei

答えて

10

kshにこれらが含まれていないことがわかったとき、私は自分自身を書きました。私は~/bin/dirstack.kshでこれを入れて、私の.kshrcファイルは次のようにそれが含まれています。ここでは

. ~/bin/dirstack.ksh 

dirstack.kshの内容は以下のとおりです。

# Implement a csh-like directory stack in ksh 
# 
# environment variable dir_stack contains all directory entries except 
# the current directory 

unset dir_stack 
export dir_stack 


# Three forms of the pushd command: 
# pushd  - swap the top two stack entries 
# pushd +3  - swap top stack entry and entry 3 from top 
# pushd newdir - cd to newdir, creating new stack entry 

function pushd 
{ 
    sd=${#dir_stack[*]} # get total stack depth 
    if [ $1 ] ; then 
     if [ ${1#\+[0-9]*} ] ; then 
     # ======= "pushd dir" ======= 

     # is "dir" reachable? 
     if [ `(cd $1) 2>/dev/null; echo $?` -ne 0 ] ; then 
      cd $1    # get the actual shell error message 
      return 1   # return complaint status 
     fi 

     # yes, we can reach the new directory; continue 

     ((sd = sd + 1))  # stack gets one deeper 
     dir_stack[sd]=$PWD 
     cd $1 
     # check for duplicate stack entries 
     # current "top of stack" = ids; compare ids+dsdel to $PWD 
     # either "ids" or "dsdel" must increment with each loop 
     # 
     ((ids = 1))   # loop from bottom of stack up 
     ((dsdel = 0))  # no deleted entries yet 
     while [ ids+dsdel -le sd ] ; do 
      if [ "${dir_stack[ids+dsdel]}" = "$PWD" ] ; then 
       ((dsdel = dsdel + 1)) # logically remove duplicate 
      else 
       if [ dsdel -gt 0 ] ; then  # copy down 
        dir_stack[ids]="${dir_stack[ids+dsdel]}" 
       fi 
       ((ids = ids + 1)) 
      fi 
     done 

     # delete any junk left at stack top (after deleting dups) 

     while [ ids -le sd ] ; do 
      unset dir_stack[ids] 
      ((ids = ids + 1)) 
     done 
     unset ids 
     unset dsdel 
     else 
     # ======= "pushd +n" ======= 
     ((sd = sd + 1 - ${1#\+})) # Go 'n - 1' down from the stack top 
     if [ sd -lt 1 ] ; then ((sd = 1)) ; fi 
     cd ${dir_stack[sd]}   # Swap stack top with +n position 
     dir_stack[sd]=$OLDPWD 
     fi 
    else 
     # ======= "pushd" ======= 
     cd ${dir_stack[sd]}  # Swap stack top with +1 position 
     dir_stack[sd]=$OLDPWD 
    fi 
} 

function popd 
{ 
    sd=${#dir_stack[*]} 
    if [ $sd -gt 0 ] ; then 
     cd ${dir_stack[sd]} 
     unset dir_stack[sd] 
    else 
     cd ~ 
    fi 
} 

function dirs 
{ 
    echo "0: $PWD" 
    sd=${#dir_stack[*]} 
    ((ind = 1)) 
    while [ $sd -gt 0 ] 
    do 
     echo "$ind: ${dir_stack[sd]}" 
     ((sd = sd - 1)) 
     ((ind = ind + 1)) 
    done 
} 
+0

これは最も有望な応答のようです、私は昼食の後に試してみます – hhafez

+0

私はそれがあなたのために働くことを願っています。私は10年以上それを使ってきました。 csh/bashに組み込まれているpushd/popd/dirの動作を正確に再現するわけではありませんが、非常に近いです。 – Eddie

+0

非常に良い。私は今これを見つけ、私が扱っているkshシステムの標準として設定します。 :) –

2

manページからの説明です:

(cd tmp; echo "test" >tmpfile) 

これはtmpディレクトリに変更し、そのディレクトリ内tmpfileというファイルを作成します。サブシェルが戻ると、現在のディレクトリは、サブシェルが起動する前の状態に復元されます。これは、各シェルインスタンスが "現在のディレクトリ"とは何かという独自の考え方を持ち、サブシェル内の現在のディレクトリを変更しても、それを呼び出すシェルには影響しないからです。

0

システムがpushdコマンドを認識できない場合は、profile.kshファイルを調べて、dir.kshの呼び出しが含まれていることを確認してください。

+0

dir.kshは何ですか? – hhafez

+0

pushdは、$ ROOTDIR/etc/dir.kshファイルで定義されているシェル関数です。 –

+0

私は便利なSolarisサーバーではありません。まず、$ ROOTDIRが定義されておらず、2番目の/ etcには* .kshファイルがありません。 – Eddie

3

あなただけのバックの単一レベルがあなたを追跡してOKであればできるエイリアスを'cd - 'または 'cd $ OLDPWD'をポップします。 dir.kshについては

...グーグルによると、それはcommercial packageの一部です:

NOTE

POPD Kornシェルの機能がこのファイルに

$ROOTDIR/etc/dir.ksh. 

を を定義していますファイルは通常 ファイル$ ROOTDIR/etc/profile.kshの処理中に ログインシェルによって処理されます。 システムが popdコマンドを認識できない場合は、profile.ksh ファイルを調べて、dir.ksh の呼び出しが含まれていることを確認します。エンタープライズ開発者のためのエンタープライズ開発者MKS Toolkitの ため プロの開発者MKS Toolkitの ため 相互運用MKS Toolkitの開発者のMKS Toolkitのシステム管理者MKS ToolkitのパワーユーザーのMKS Toolkitの

可用性

MKS Toolkitの64 Bit 版

+0

私はcdを使いますが、時には私はもっと必要です:) – hhafez

+1

名前 'dir.sh'はどこにでもあることができるほど一般的です。さらに、私はMKSツールキットがこれ以上存在しないと思います。 – Mei