0

我构建了这个数组来列出目录的内容,允许用户输入数字而不是确切的目录名称。我不知道如何从只有数字的输入中获取选择的目录名称?我需要它来确定从将要使用的数组中选择的确切目录。感谢所有愿意提供帮助的人。

if [ -d $TDDIR ]; then
# List folders in $TDDIR
VAR1=1
FILES=( `ls $TDDIR` )
FILECNT=${#FILES[*]}
# List number of folders available
clear
echo "Current list of folders to pick from."
echo
while [ $VAR1 -lt $FILECNT ]; do 
    echo -e "[$VAR1] \t ${FILES[VAR1]}"
    let "VAR1 += 1"
done
# Reduce VAR1 by one
let "VAR1 -= 1" 
# Have user choose which to use for scan
read -p "Which file would you like to scan 1 - $VAR1 or 0 to exit? :" INPUT
  if [ $INPUT -eq '0' ]; then 
    echo "You chose to quit. Exiting now." 
    sleep 2
    exit 0
  fi
while (( $INPUT > $FILECNT || $INPUT < 1 || $INPUT > $VAR1 )); do
echo
echo "You selected $INPUT."
read -p "$INPUT is not valid.  Choose a valid option: " INPUT
done
else    exit
fi
4

1 回答 1

1
echo ${FILES[(($VAR - 1))]}

-1 是因为 bash 数组的索引从 0 开始。

附带说明, ls 不适合该任务(请参阅:解析 ls 输出s)

此外,您可以尝试使用 select 作为菜单(请参阅:Bash Script Menus and Functions

于 2013-02-28T07:22:12.813 回答