Advertisement
MestreLion

Useful menu and interactive snippets

Aug 5th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # useful snippets for menus and user-interactive scripts. For reference only
  4. # All will loop until user chooses to quit (blank, menu choice, cancel)
  5.  
  6. # Read
  7. while read -r -p "Tell me your city (ENTER to quit): " city && [[ $city ]]; do
  8.     echo "You live in $city"
  9. done
  10.  
  11. # One-liner (for pasting in #bash)
  12. while read -r -p "Name (ENTER to quit): " name && [[ $name ]]; do { echo "Hello $name"; } done
  13.  
  14.  
  15. # Array of options for select. Globs can be used for files
  16. options=("AAA" "BBB" "CCC")
  17.  
  18.  
  19. # Select Method 1 (fancier): add a last entry for Quit
  20.  
  21. # Only way to customoze select prompt is via PS3
  22. PS3="Choose an option: "
  23.  
  24. # Select the with options, with an n+1 "Quit" entry added
  25. echo "Select exemple 1:"
  26. select entry in "${options[@]}" "Quit"; do
  27.  
  28.     # Test for last entry (size of options +1) and exit select (break)
  29.     (( "$REPLY" != "${#options[@]}"+1 )) || { echo "Goodbye!" ; break; }
  30.  
  31.     # Test invalid options ($entry="") and try again (continue)
  32.     [[ "$entry" ]] || { echo "Invalid option" ; continue; }
  33.  
  34.     #do stuff
  35.     echo "You picked $entry which is option $REPLY"
  36.  
  37. done
  38.  
  39.  
  40. # Method 2 (simpler): Non-existant entry 0 for quit
  41. echo "Select exemple 2:"
  42. PS3="Pick a choice (0 to quit): "
  43. select entry in "${options[@]}"; do
  44.  
  45.     [[ ! "$REPLY" = "0" ]] || { echo "Goodbye!" ; break; }
  46.     [[ "$entry" ]] || { echo "Invalid option" ; continue; }
  47.  
  48.     echo "You picked $entry which is option $REPLY"
  49.  
  50. done
  51.  
  52.  
  53. # Method 1 with case
  54.  
  55. title="Select example"
  56. prompt="Pick an option:"
  57.  
  58. echo "$title"
  59. PS3="$prompt "
  60. select opt in "${options[@]}" "Quit"; do
  61.  
  62.     case "$REPLY" in
  63.  
  64.     1 ) echo "You picked $opt which is option $REPLY";;
  65.     2 ) echo "You picked $opt which is option $REPLY";;
  66.     3 ) echo "You picked $opt which is option $REPLY";;
  67.  
  68.     $(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
  69.     *) echo "Invalid option. Try another one.";continue;;
  70.  
  71.     esac
  72.  
  73. done
  74.  
  75.  
  76. # Loop with zenity (GUI)
  77.  
  78. while opt=$(zenity --title="$title" --text="$prompt" --list --column="Options" "${options[@]}"); do
  79.  
  80.     case "$opt" in
  81.     "${options[0]}" ) zenity --info --text="You picked $opt which is option 1";;
  82.     "${options[1]}" ) zenity --info --text="You picked $opt which is option 2";;
  83.     "${options[2]}" ) zenity --info --text="You picked $opt which is option 3";;
  84.     *) zenity --error --text="Invalid option. Try another one.";;
  85.     esac
  86.  
  87. done
  88.  
  89.  
  90. # Select with case (upon user request in #bash)
  91.  
  92. list=("Christmas" "Independence Day" "Thanksgiving" "Easter" "Memorial Day")
  93. echo "Holliday selector"
  94. PS3="Pick your favorite holliday (0 to quit): "
  95. select entry in "${list[@]}"; do
  96.  
  97.     case "$REPLY" in
  98.     0) echo "Bye sucker!"; break;;
  99.     1) echo "$entry: Merry Chri'mah, bitch!";;
  100.     2) echo "$entry: Party hard, nigga!";;
  101.     3) echo "$entry: Happy Turkey Day! (now go get fat)";;
  102.     4) echo "$entry: Jesus done come back to life, fools!!";;
  103.     5) echo "$entry: Summer's here, you wankers!! School's OUT!";;
  104.     *) echo "Sorry. The programmer was lazy and didn't list that many holidays. Try another one.";continue;;
  105.     esac
  106.  
  107. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement