Last active
May 22, 2025 21:10
-
-
Save yannxou/bcfc7b6282dd496d23fc8b3ab5935753 to your computer and use it in GitHub Desktop.
Bash script to select from multiple options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # customize with your own. | |
| options=("AAA" "BBB" "CCC" "DDD") | |
| menu() { | |
| clear | |
| echo "Avaliable options:" | |
| for i in ${!options[@]}; do | |
| printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}" | |
| done | |
| [[ "$msg" ]] && echo "$msg"; : | |
| } | |
| prompt="Check an option (again to uncheck, ENTER when done, Q to quit): " | |
| while menu && read -rp "$prompt" num && [[ "$num" ]]; do | |
| case $num in | |
| q|Q) exit 0 ;; | |
| esac | |
| [[ "$num" != *[![:digit:]]* ]] && | |
| (( num > 0 && num <= ${#options[@]} )) || | |
| { msg="Invalid option: $num"; continue; } | |
| ((num--)); msg="" # msg="${options[num]} was ${choices[num]:+un}checked" | |
| [[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+" | |
| done | |
| printf "You selected"; msg=" nothing" | |
| for i in ${!options[@]}; do | |
| [[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; msg=""; } | |
| done | |
| echo "$msg" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment