#!/usr/bin/env sh # Define ask_Yn, which asks a yes or no question and saves the result in the given variable name (defaults to yes) # Takes one arg, which is the variable name where the result will be saved # No is 0 and Yes is 1 source say ask_Yn() { if [ -n "$2" ]; then say_inline "$2 (Y/n): " fi STTY_CONFIG=$(stty -g) # Save current stty config stty raw -echo # Disable echo ANSWER=$(head -c 1) # Scrape terminal input stty $STTY_CONFIG # Restore stty config if [ "$ANSWER" != "${ANSWER#[Nn]}" ]; then say "N" export "${1}=0" else say "Y" export "${1}=1" fi } # Check if the script is being called from another script if [ "${BASH_SOURCE[0]}" = "$0" ]; then # If not, call the function ask_Yn "$@" fi