我正在使用这样的bash脚本菜单:
#!/bin/bash
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice 3"
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
每次菜单选择后,我都会收到提示
Please enter your choice:
如何在每个选项执行完毕后始终显示菜单?我做了一些环顾四周,我想我可以做一些while循环,但我还没有能够得到任何工作。
答案 0 :(得分:14)
使它美观且用户友好; - )
#!/bin/bash
while :
do
clear
cat<<EOF
==============================
Menusystem experiment
------------------------------
Please enter your choice:
Option (1)
Option (2)
Option (3)
(Q)uit
------------------------------
EOF
read -n1 -s
case "$REPLY" in
"1") echo "you chose choice 1" ;;
"2") echo "you chose choice 2" ;;
"3") echo "you chose choice 3" ;;
"Q") exit ;;
"q") echo "case sensitive!!" ;;
* ) echo "invalid option" ;;
esac
sleep 1
done
将此示例中的回声替换为函数调用或对其他脚本的调用。
答案 1 :(得分:5)
刚修改了你的脚本,如下所示。它为我工作!
#!/bin/bash
while true
do
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
break
;;
"Option 2")
echo "you chose choice 2"
break
;;
"Option 3")
echo "you chose choice 3"
break
;;
"Quit")
echo "Thank You..."
exit
;;
*) echo invalid option;;
esac
done
done
答案 2 :(得分:0)
您还可以执行以下操作:
#!/bin/bash
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice 3"
;;
"Quit")
break
;;
*) echo invalid option;;
esac
counter=1
SAVEIFS=$IFS
IFS=$(echo -en "\n\b") # we set another delimiter, see also: https://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
for i in ${options[@]};
do
echo $counter')' $i
let 'counter+=1'
done
IFS=$SAVEIFS
done