8

KSH のコマンドで bash pushd/popd ビルドに相当するものはありますか?

bash の pushd と popd が何をするかわからない人のために、man ページの説明を以下に示します。

   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:

ありがとう

4

5 に答える 5

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
}
于 2009-06-12T01:44:39.767 に答える
4

単一レベルのバック トラッキングで問題ない場合は、'cd -' または 'cd $OLDPWD' を popd にエイリアスできます。

dir.kshについては... Googleによると、商用パッケージの一部です:

ノート

popd は、ファイルで定義された KornShell 関数です。

$ROOTDIR/etc/dir.ksh.

このファイルは通常、ファイル $ROOTDIR/etc/profile.ksh の処理中にログイン シェルによって処理されます。システムが popd コマンドを認識できない場合は、profile.ksh ファイルを調べて、dir.ksh への呼び出しが含まれていることを確認してください。

可用性

パワー ユーザー向け MKS ツールキット システム管理者向け MKS ツールキット 開発者向け MKS ツールキット 相互運用性向け MKS ツールキット プロフェッショナル開発者向け MKS ツールキット エンタープライズ開発者向け MKS ツールキット エンタープライズ開発者向け MKS ツールキット 64 ビット版

于 2009-06-12T02:41:03.000 に答える
2

私は通常、この種のサブシェルを使用します。

(cd tmp; echo "test" >tmpfile)

これにより、ディレクトリが変更され、そのディレクトリtmpに呼び出されるファイルが作成されますtmpfile。サブシェルが戻った後、現在のディレクトリはサブシェルが開始される前の状態に復元されます。これは、各シェル インスタンスが「現在のディレクトリ」を独自に認識しており、サブシェルで現在のディレクトリを変更しても、それを呼び出したシェルには影響しないためです。

于 2009-06-12T01:38:33.353 に答える
0

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

于 2009-06-12T01:40:44.237 に答える