У меня есть следующий сценарий:
#!/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
Моя проблема состоит в том, что я не знаю, как удалить выбор из списка после того, как он был выбран. Это возможно? Как?
Самый простой путь состоит в том, чтобы использовать 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.