質問
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:
ありがとう
解決
、私は自分自身を書きました。私は~/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
}
他のヒント
あなたは、別名あなたにできるバックトラッキングの1つだけのレベルでOKであれば「CD - 」。POPDするか、「CD $ OLDPWD」
dir.kshについては...グーグルによると、それは商用パッケージするます:
注
POPDは、Kornシェル関数が定義されています ファイルの
$ROOTDIR/etc/dir.ksh.
このファイルは通常によって処理されます の処理中にログインシェル ファイル$ ROOTDIRは/ etc / profile.ksh。もし お使いのシステムが認識できません POPDコマンド、あなたのprofile.kshをチェック 呼び出しがdir.kshすることを保証するために、ファイル 含まれます。
可用性
パワーユーザーのためのMKSのMKS Toolkit システム管理者のMKS Toolkitのための 以下のための開発者のMKS Toolkitのツールキット 相互運用性のためのMKS Toolkit プロの開発者のMKS Toolkit エンタープライズ開発者MKS Toolkitの エンタープライズ開発者のための64ビット 版
私は通常、この種のもののためのサブシェルを使用します:
(cd tmp; echo "test" >tmpfile)
このはtmp
ディレクトリに変更し、そのディレクトリ内のtmpfile
というファイルを作成します。サブシェルが戻った後、現在のディレクトリがサブシェルが起動する前にそれが何だったかに復元されます。各シェルインスタンスは「カレントディレクトリ」は何であるかの独自の考えを持っており、サブシェルでカレントディレクトリを変更すると、それを呼び出したシェルには影響を与えないためです。
受け入れ答えでの微妙なバグがあります。 「のpushdは」いいえ引数($ 1 =「」)で呼び出された場合のうち、「ポップと空のdir_stack、それはすることができないと述べたスタックに空白のエントリを注入します」。エッジケースをキャッチすると、それを修正するようです。
ここで修正されたコードです。 EDIT:どこに(/バッシュPUSHD W一致)プッシュしないようにstderrに訴えます。私はそれが改善だと感じて「の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
}
システムが Pushd コマンドを認識できない場合は、profile.ksh ファイルを調べて、dir.ksh への呼び出しが含まれていることを確認してください。