如果我想从用户键盘输入验证null返回,如果用户没有选择重新打印菜单的选项怎么能实现呢?如果无效选择清除屏幕/重新打印菜单但确保错误消息仍然可见?也许捕获一个返回代码作为退出的一部分,并基于提供错误?
文件ALIAS_FILE.config包含以下值:
ALIAS1 CLUSTER1
ALIAS2 CLUSTER2
ALIAS3 CLUSTER3
QUIT
脚本:
mapfile -t arr < "/var/ALIAS_FILE.config"
select alias_select in "${arr[@]}"
do
if [[ -z "$alias_select" ]]
then
echo
echo "Invalid Choice"
echo
elif [[ $alias_select == "QUIT" ]]
then
break
else
echo
echo "You selected: $alias_select"
echo
ALIAS=$(echo "${alias_select}" | awk '{print $1}')
CLUSTER=$(echo "${alias_select}" | awk '{print $2}')
echo "$ALIAS"
echo "$CLUSTER"
fi
done
答案 0 :(得分:0)
只要你有你的文件(/var/ALIAS_FILE.config
),你就可以随时显示菜单,你也可以通过mapfile
访问arr中的值,这是一个快速尝试,一些建议:
echo -e
或事件(并推荐)printf
printf "\033c"
更新后的脚本:
#!/bin/bash
mapfile -t arr< "/var/ALIAS_FILE.config"
print_menu(){
for (( i=0; i<${#arr[@]}; i++ ));
do
echo $i")" ${arr[$i]}
done
}
printf "\033c"
select alias_select in "${arr[@]}"
do
printf "\033c"
if [[ -z "$alias_select" ]]
then
echo -e "\nInvalid Choice\n"
print_menu
elif [[ $alias_select == "QUIT" ]]
then
break
else
echo -e "\nYou selected: $alias_select\n"
echo -e "${alias_select// *}\n${alias_select##* }"
fi
done