fork(2) download
  1. # This file provides some common functionality for the main scripts. It is
  2. # designed to be sourced, rather than executed.
  3.  
  4. # Makes the script more robust in general.
  5. set -o errexit
  6. set -o nounset
  7. set -o pipefail
  8.  
  9. # ANSI escape codes for pretty terminal colors.
  10. readonly GREEN='\033[32;1m'
  11. readonly YELLOW="\033[33;1m"
  12. readonly RED="\033[31;1m"
  13. readonly RESET="\033[0m"
  14.  
  15. # Gives the user a "yes/no" prompt with the text from the first positional
  16. # parameter (if given). If an affirmative response is received,
  17. # returns success; if a negative response is received, returns failure; if an
  18. # unrecognized response is received, the user is given the prompt again. This
  19. # function always outputs to the terminal rather than stdout, so it is safe
  20. # to use from within functions that are expected to output their results.
  21. Prompt()
  22. {
  23. # Only try to output the prompt message if it is given, so no newline is
  24. # printed if it isn't.
  25. [ $# -gt 0 ] && echo -e "$1" > /dev/tty
  26.  
  27. local Response
  28. echo -en "[${GREEN}Y${RESET}/${RED}N${RESET}] " > /dev/tty
  29. read Response
  30.  
  31. # For case insensitivity.
  32. Response="$(echo "$Response" | tr [A-Z] [a-z])"
  33.  
  34. if [[ "$Response" =~ ^y(es)?$ ]]; then true
  35. elif [[ "$Response" =~ ^no?$ ]]; then false
  36. else
  37. echo "I don't understand that response. Try \"yes\" or \"no.\"" > /dev/tty
  38. # Keep pestering the user until he gives a valid response.
  39. Prompt
  40. fi
  41. }; readonly -f Prompt
  42.  
  43. # Outputs a string of shell code that will parse the program arguments for long options.
  44. # Example:
  45. # # Let's say that the program was passed the arguments --One Two --Three.
  46. # eval "$(OptionParser One Two Three)"
  47. # echo $One # True.
  48. # echo $Two # False. The argument was not preceded by "--" on the command-line.
  49. # echo $Three # False. OptionParser stops on the first argument that isn't an
  50. # # option, and Two wasn't an option.
  51. OptionParser()
  52. {
  53. if [ $# -ge 1 ]; then
  54. for OptionName in "$@"; do echo -n "$OptionName=false; "; done
  55. echo -n 'while [ $# -ge 1 ]; do case "$1" in'
  56. for OptionName in "$@"; do
  57. echo -n " '--$OptionName') $OptionName=true && shift ;;"
  58. done
  59. echo -n ' *) break ;; esac; done'
  60. fi
  61. }; readonly -f OptionParser
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty