我有以下脚本:
#!/bin/bash
# Bash Menu Script Example
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
done
我的问题是,我不知道如何在选择后从列表中删除选项。这可能吗?怎么做?
答案1
最简单的方法是使用unset
:
$ options=(aa bb cc dd)
$ echo ${options[@]}
aa bb cc dd
## Remove the 3d element of the array (arrays start at 0)
$ unset options[2]
$ echo ${options[@]}
aa bb dd
有关详细信息,请参阅help unset
:
unset: unset [-f] [-v] [name ...]
Unset values and attributes of shell variables and functions.
For each NAME, remove the corresponding variable or function.
Options:
-f treat each NAME as a shell function
-v treat each NAME as a shell variable
Without options, unset first tries to unset a variable, and if that fails,
tries to unset a function.
Some variables cannot be unset; also see `readonly'.
Exit Status:
Returns success unless an invalid option is given or a NAME is read-only.