Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash ksh上的pushd/popd?_Bash_Ksh - Fatal编程技术网

Bash ksh上的pushd/popd?

Bash ksh上的pushd/popd?,bash,ksh,Bash,Ksh,KSH是否有类似于bash pushd/popd的内置命令 对于那些不知道bash中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 curre

KSH是否有类似于bash pushd/popd的内置命令

对于那些不知道bash中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:

谢谢

对于这类事情,我通常使用子shell:

(cd tmp; echo "test" >tmpfile)

这将更改为
tmp
目录,并在该目录中创建名为
tmpfile
的文件。子shell返回后,当前目录将恢复到子shell启动前的状态。这是因为每个shell实例都对“当前目录”有自己的概念,并且更改子shell中的当前目录不会影响调用它的shell。

如果系统无法识别pushd命令,请检查profile.ksh文件以确保包含对dir.ksh的调用

当我发现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
}

如果您只接受一个级别的回溯,则可以将“cd-”或“cd$OLDPWD”别名为popd

至于ksh局长。。。据谷歌称,这是一个:

popd是一个定义为KornShell函数的函数 在文件中

$ROOTDIR/etc/dir.ksh.
此文件通常由 在处理过程中登录shell 文件$ROOTDIR/etc/profile.ksh。如果 您的系统无法识别错误 popd命令,检查您的profile.ksh 文件以确保调用dir.ksh 包括在内

可用性

面向超级用户的MKS工具包MKS 系统管理员工具包MKS 开发者工具包MKS开发者工具包 互操作性MKS工具包 专业开发人员MKS工具包 面向企业开发人员的MKS工具包 适用于企业开发人员64位 版本


电脑里有一个微妙的错误。当在没有arg($1=”“)和空dir_堆栈的情况下调用“pushd”时,它会向所述堆栈中注入一个无法“弹出”的空白条目。抓住边缘案例似乎可以解决问题

这是正确的代码。编辑:无处推送时向stderr投诉(与bash pushd一致)。将索引列表保留在“dirs”上,因为我觉得这是一种改进:')


pushd是在文件$ROOTDIR/etc/dir.ksh中定义的shell函数。在我手头的任何Solaris服务器上都没有。首先,我没有定义$ROOTDIR,其次,/etc中没有任何*.ksh文件。为了再次检查,我选中了Solaris 7和Solaris 10框,所以我选中了多个版本。您的dir.ksh必须是本地定义的,或者您可能没有使用Solaris。在这种情况下,它是特定于操作系统的,而不是特定于机器的。确切地说,dir.ksh不在我的系统上,因此它可能是特定于操作系统的,正如您所说。这似乎是最有希望的回答,我将在午餐后试一试。我希望它对您有效。我已经用了十多年了。它并没有完全重现csh/bash中内置的pushd/popd/dirs的行为,但是非常接近,非常好。我今天刚刚发现了这一点,并将其设置为我使用的ksh系统的标准配置。:)我使用cd,但有时我需要更多:)名称
dir.sh
非常通用,可能到处都是。更重要的是,我认为MKS工具包已经不存在了。Oracle的一个博客中出现了一个优秀的
pushd
/
popd
安装,可能与它在Solaris中的使用有关。
# 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
}