Advertisement
MestreLion

checkboxes in bash

Aug 5th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.96 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Lazy "checkboxes" in bash script
  4. # (for those who dont want to use whiptail)
  5.  
  6. # My most complex (and cryptic... and useless) pure bash script so far
  7. # answers http://serverfault.com/questions/144939
  8.  
  9. options=("AAA" "BBB" "CCC" "DDD")
  10.  
  11. menu() {
  12.     echo "Avaliable options:"
  13.     for i in ${!options[@]}; do
  14.         printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${options[i]}"
  15.     done; [[ "$msg" ]] && echo "$msg"; :
  16. }
  17.  
  18. prompt="Check an option (again to uncheck, ENTER when done): "
  19. while menu && read -rp "$prompt" num && [[ "$num" ]]; do
  20.     [[ "$num" != *[![:digit:]]* ]] && (( num > 0 && num <= ${#options[@]} )) || {
  21.         msg="Invalid option: $num"; continue
  22.     }
  23.     ((num--)); msg="${options[num]} was ${choices[num]:+un}checked"
  24.     [[ "${choices[num]}" ]] && choices[num]="" || choices[num]="+"
  25. done
  26.  
  27. printf "You selected"; msg=" nothing"
  28. for i in ${!options[@]}; do
  29.     [[ "${choices[i]}" ]] && { printf " %s" "${options[i]}"; msg=""; }
  30. done; echo "$msg"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement