當在遞歸查看目錄時,我在ksh93中遇到了一些問題。在ksh93中使用遞歸時出現的奇怪行爲
創建一些文件和目錄。
base=/tmp/nonsens
for i in {1..3}
do
mkdir -p ${base}/dir${i}
for j in {1..2}
do
mkdir ${base}/dir${i}/dir${j}
touch ${base}/dir${i}/dir${j}/file${j}
touch ${base}/dir${i}/file${j}
done
done
現在通過它與ksh93腳本。
rdir()
{
typeset dir=$1
for file in `ls $dir`
do
if [ -d $dir/$file ]
then
echo "Directory: $dir/$file"
rdir $dir/$file
else
echo "File : $dir/$file"
fi
done
}
rdir /tmp/nonsens
將同時使用了pdksh創建ksh93的
[email protected]:~> rdir /tmp/nonsens
Directory: /tmp/nonsens/dir1
Directory: /tmp/nonsens/dir1/dir1
File : /tmp/nonsens/dir1/dir1/file1
File : /tmp/nonsens/dir1/dir1/dir2
File : /tmp/nonsens/dir1/dir1/file1
File : /tmp/nonsens/dir1/dir1/file2
File : /tmp/nonsens/dir1/dir1/dir2
File : /tmp/nonsens/dir1/dir1/dir3
這個輸出/ bash將創建這個
[email protected]:~$ rdir /tmp/nonsens
Directory: /tmp/nonsens/dir1
Directory: /tmp/nonsens/dir1/dir1
File : /tmp/nonsens/dir1/dir1/file1
Directory: /tmp/nonsens/dir1/dir2
File : /tmp/nonsens/dir1/dir2/file2
File : /tmp/nonsens/dir1/file1
File : /tmp/nonsens/dir1/file2
Directory: /tmp/nonsens/dir2
Directory: /tmp/nonsens/dir2/dir1
File : /tmp/nonsens/dir2/dir1/file1
Directory: /tmp/nonsens/dir2/dir2
File : /tmp/nonsens/dir2/dir2/file2
File : /tmp/nonsens/dir2/file1
File : /tmp/nonsens/dir2/file2
Directory: /tmp/nonsens/dir3
Directory: /tmp/nonsens/dir3/dir1
File : /tmp/nonsens/dir3/dir1/file1
Directory: /tmp/nonsens/dir3/dir2
File : /tmp/nonsens/dir3/dir2/file2
File : /tmp/nonsens/dir3/file1
File : /tmp/nonsens/dir3/file2
是否有人知道一個解決方法嗎?或者是否存在一個開關,使ksh93的行爲,因爲它應該?