我正在编写一个ksh函数(放置在.profile文件中),它将显示一个子目录菜单并允许用户选择要cd进入的一个。这是代码:
# 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 shell 变量(默认包含空格、制表符和换行符)设置为单个换行符:
IFS=`echo` # echo outputs a trailing newline character by default
这似乎完成了预期的验证:
echo -e "$IFS\c" | hexdump -c
但是,尽管我尽了最大努力(在几天的工作过程中),但我未能将包含空格的整个目录名称设置为位置参数的值。
我错过了什么?
特此征求并欢迎提出建议。
ADVA谢谢NCE
鲍勃