サブディレクトリのメニューを表示し、ユーザーがcdするディレクトリを選択できるようにするksh関数 ( .profileファイルに配置される) を作成しています。コードは次のとおりです。
# Menu driven subdirectory descent.
d(){
# Only one command line argument accepted
[ "$1" = "--" ] && shift $# # Trap for "ls --" feature
wd=`pwd`; arg="${1:-$wd}"
dirs="`/bin/ls -AF $arg 2>/dev/null | grep /$ | tr -d \"/\"`"
# Set the names of the subdirectories to positional parameters
if [ "$dirs" ] ;then
set $dirs
if [ $# -eq 1 -a "$arg" = "$wd" ] ;then cd $arg/$1; return; fi # trap: it's obvious; do it
else echo "No subdirectories found" >&2; return 1
fi
# Format and display the menu
if [ `basename "${arg}X"` = "${arg}X" ] ;then arg="$wd/$arg"; fi # Force absolute path if relitive
echo -e "\n\t\tSubdirectories relative to ${arg}: \n"
j=1; for i; do echo -e "$j\t$i"; j=`expr $j + 1`; done | pr -r -t -4 -e3
echo -e "\n\t\tEnter the number of your choice -- \c "
# Convert user-input to directory-name and cd to it
read choice; echo
dir=`eval "(echo $\{"$choice"\})"` # Magic here.
[ "$choice" -a "$choice" -ge 1 -a "$choice" -le "$#" ] && cd $arg/`eval echo "$dir"`
}
この関数は、スペース文字を含むディレクトリ名を除いて、かなりうまく機能します。ディレクトリ名にスペースが含まれている場合、setコマンドはディレクトリ名の (完全なディレクトリ名ではなく) スペースで区切られた各要素を個別の定位置パラメーターに設定します。それはここでは役に立ちません。
$IFSシェル変数 (デフォルトでspace、tab、およびnewlineを含む) を単一の改行文字に設定しようとしました:
IFS=`echo` # echo outputs a trailing newline character by default
これは、次の方法で検証された意図を達成しているように見えます。
echo -e "$IFS\c" | hexdump -c
しかし、私の最善の努力にもかかわらず (数日間の作業の過程で)、スペースを含むディレクトリ名全体を位置パラメーターの値として設定できませんでした。
私は何が欠けていますか?
提案はここに求められ、大歓迎です。
ADVAありがとうNCE
ボブ