在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:谢谢
发布于 2009-06-12 01:44:40
当我发现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-12 02:41:03
如果您可以只使用一个级别的回溯,您可以将'cd -‘或'cd $OLDPWD’别名为popd。
至于dir.ksh...根据谷歌的说法,它是commercial package的一部分
笔记
popd是文件中定义的KornShell函数
$ROOTDIR/etc/dir.ksh。
此文件通常在处理$ROOTDIR/etc/profile.ksh文件的过程中由登录shell处理。如果您的系统无法识别dir.ksh命令,请检查您的popd文件以确保包含对popd的调用。
可用性
面向高级用户的MKS工具包面向系统管理员的MKS工具包面向开发人员的MKS工具包面向互操作性的MKS工具包面向专业开发人员的MKS工具包面向企业开发人员的MKS工具包面向企业开发人员的MKS工具包64位版
发布于 2009-06-12 01:38:33
我通常使用subshell来处理这类事情:
(cd tmp; echo "test" >tmpfile)这将切换到tmp目录,并在该目录中创建一个名为tmpfile的文件。子subshell返回后,当前目录将恢复为子subshell启动之前的目录。这是因为每个shell实例都对“当前目录”有自己的概念,并且更改子shell中的当前目录不会影响调用它的shell。
https://stackoverflow.com/questions/984635
复制相似问题