문제

KSH 명령에 Bash Pushd/Popd 빌드와 동일합니까?

Bash에서 무엇을 밀고 popd하는지 모르는 사람들을 위해 Man Page의 설명은 다음과 같습니다.

   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:

감사

도움이 되었습니까?

해결책

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
}

다른 팁

단일 수준의 백 추적만으로도 괜찮다면 alias 'cd-'또는 'cd $ oldpwd'를 popd로 만들 수 있습니다.

Dir.ksh ... Google에 따르면 그것은 A의 일부입니다. 상업용 패키지:

노트

POPD는 파일에 정의 된 Kornshell 함수입니다

$ROOTDIR/etc/dir.ksh.

이 파일은 일반적으로 $ rootdir/etc/profile.ksh 파일을 처리하는 동안 로그인 쉘에 의해 처리됩니다. 시스템이 POPD 명령을 인식하지 못하면 Profile.ksh 파일을 확인하여 Dir.ksh에 대한 호출이 포함되어 있는지 확인하십시오.

유효성

MKS 툴킷 파워 사용자 MKS 툴킷 시스템 관리자 MKS 툴킷 MKS 툴킷 개발자 MKS 툴킷 상호 운용성 MKS 툴킷 전문 개발자 용 MKS 툴킷 엔터프라이즈 개발자 MKS 툴킷 엔터프라이즈 개발자 용 MKS 툴킷 64 비트 에디션

나는 보통 이런 종류의 일에 대한 서브 쉘을 사용합니다.

(cd tmp; echo "test" >tmpfile)

이것은로 변경됩니다 tmp 디렉토리와 호출 된 파일을 만듭니다 tmpfile 그 디렉토리에서. 서브 쉘 반환 후 현재 디렉토리는 서브 쉘이 시작되기 전까지 복원됩니다. 이는 각 쉘 인스턴스가 "현재 디렉토리"가 무엇인지에 대한 자체 아이디어를 가지고 있기 때문에, 서브 쉘에서 현재 디렉토리를 변경하는 것은 그것을 불리는 쉘에 영향을 미치지 않기 때문입니다.

미묘한 버그가 있습니다 받아 들여진 답변. '푸시'가 Arg ($ 1 = "" ")없이 호출되면 비어있는 Dir_stack이 발생하면 해당 스택에 빈 항목을 주입하여"팝업 "할 수 없습니다. 에지 케이스를 잡는 것은 그것을 고치는 것 같습니다.

수정 된 코드는 다음과 같습니다. 편집 : 누가 밀기가없는 경우 Stderr에게 불만을 제기합니다 (bash 푸시가 일관되게). 개선이라고 생각하면서 'Dirs'에 인덱싱 된 목록을 남겼습니다. ')

# 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
}

시스템이 푸시드 명령을 인식하지 못하면 프로파일을 확인하십시오. KSH 파일을 확인하여 Dir.ksh 로의 호출이 포함되어 있는지 확인하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top