Question

Y at-il un équivalent de la construction pushd / popd bash dans les commandes pour le KSH?

Pour ceux qui ne savent pas ce que pushd et popd en bash faire, voici la description de la page 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:

Merci

Était-ce utile?

La solution

Quand j'ai découvert que ksh ne comprenait pas ceux-ci, je l'ai écrit moi-même. Je mets cela dans ~/bin/dirstack.ksh et mon fichier .kshrc inclut comme ceci:

. ~/bin/dirstack.ksh

Voici le contenu de 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
}

Autres conseils

Si vous êtes OK avec juste un seul niveau de retour vous suivi peut alias cd - '. Ou 'cd $ OLDPWD' à popd

En ce qui concerne dir.ksh ... selon Google, il fait partie d'un paquet commercial :

  

NOTE

     

popd est une fonction définie KornShell   dans le fichier

$ROOTDIR/etc/dir.ksh.
     

Ce fichier est traité par un   shell connecter pendant le traitement de   le fichier rootdir $ / etc / profile.ksh. Si   votre système ne reconnaît pas le   commande popd, vérifiez votre profile.ksh   fichier pour vous assurer qu'un appel à dir.ksh   est inclus.

     

DISPONIBILITÉ

     

MKS Toolkit pour les utilisateurs MKS   Boîte à outils pour les administrateurs système MKS   Boîte à outils pour les développeurs MKS Toolkit pour   L'interopérabilité MKS Toolkit pour   Professional Developers Toolkit MKS   Les développeurs pour l'entreprise MKS Toolkit   pour les développeurs Enterprise 64 bits   Edition

J'utilise habituellement un sous-shell pour ce genre de chose:

(cd tmp; echo "test" >tmpfile)

Cela change le répertoire de tmp et crée un fichier appelé tmpfile dans ce répertoire. Après le retour du sous-shell, le répertoire courant est rétabli à ce qu'il était avant la sous-shell a commencé. En effet, chaque instance shell a sa propre idée de ce que le « répertoire courant » est, et changer le répertoire en cours dans un sous-shell n'affecte pas la coquille qui l'a appelé.

Il y a un bug subtil dans la réponse acceptée . Lorsque « pushd » est invoqué sans arg (1 $ = « ») et un dir_stack vide, il injecte une entrée vide dans ladite pile qui ne peut pas être « sauté » sur. Attraper le cas de bord semble le fixer.

Voici le code corrigé. EDIT: se plaindre à stderr quand nulle part pour pousser (en accord avec / bash pushd). A gauche la liste indexée sur « dirs » comme je le sens est une amélioration: ')

# 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" =======
      # swap only if there's a value to swap with
      if [ ${#dir_stack[*]} = "0" ]; then
         echo "ksh: pushd: no other directory" >&2
      else
         cd ${dir_stack[sd]}       # Swap stack top with +1 position
         dir_stack[sd]=$OLDPWD
      fi
   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
}

Si votre système ne reconnaît pas la commande pushd, vérifiez votre fichier profile.ksh pour assurer qu'un appel à dir.ksh est inclus.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top