Created
January 1, 2021 05:19
-
-
Save NickHollow/107fd4f53eadd361d8b26b155278edd5 to your computer and use it in GitHub Desktop.
Revisions
-
d13r revised this gist
Nov 23, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ # gets a valid answer. ask() { # https://djm.me/ask local prompt default reply if [ "${2:-}" = "Y" ]; then -
d13r revised this gist
Sep 14, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ # gets a valid answer. ask() { # https://gist.github.com/davejamesmiller/1965569 local prompt default reply if [ "${2:-}" = "Y" ]; then -
d13r revised this gist
Sep 14, 2018 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,18 +6,18 @@ ask() { # https://djm.me/ask local prompt default reply if [ "${2:-}" = "Y" ]; then prompt="Y/n" default=Y elif [ "${2:-}" = "N" ]; then prompt="y/N" default=N else prompt="y/n" default= fi while true; do # Ask the question (not using "read -p" as it uses stderr not stdout) echo -n "$1 [$prompt] " -
d13r revised this gist
Aug 11, 2018 . 2 changed files with 37 additions and 39 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -37,42 +37,4 @@ ask() { esac done } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ # EXAMPLE USAGE: if ask "Do you want to do such-and-such?"; then echo "Yes" else echo "No" fi # Default to Yes if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" Y; then echo "Yes" else echo "No" fi # Default to No if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" N; then echo "Yes" else echo "No" fi # Only do something if you say Yes if ask "Do you want to do such-and-such?"; then said_yes fi # Only do something if you say No if ! ask "Do you want to do such-and-such?"; then said_no fi # Or if you prefer the shorter version: ask "Do you want to do such-and-such?" && said_yes ask "Do you want to do such-and-such?" || said_no -
d13r revised this gist
Oct 10, 2017 . 2 changed files with 5 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ ask() { # https://djm.me/ask local prompt default reply while true; do @@ -23,15 +23,15 @@ ask() { echo -n "$1 [$prompt] " # Read the answer (use /dev/tty in case stdin is redirected from somewhere else) read reply </dev/tty # Default? if [ -z "$reply" ]; then reply=$default fi # Check if the reply is valid case "$reply" in Y*|y*) return 0 ;; N*|n*) return 1 ;; esac -
d13r revised this gist
May 28, 2017 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ The latest version of this function can be found [on my website](https://djm.me/ask). -
d13r revised this gist
May 28, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ # gets a valid answer. ask() { # https://djm.me/ask local prompt default REPLY while true; do -
d13r revised this gist
Aug 3, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,8 @@ ask() { # http://djm.me/ask local prompt default REPLY while true; do if [ "${2:-}" = "Y" ]; then -
d13r revised this gist
May 6, 2016 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,11 @@ ask() { default= fi # Ask the question (not using "read -p" as it uses stderr not stdout) echo -n "$1 [$prompt] " # Read the answer (use /dev/tty in case stdin is redirected from somewhere else) read REPLY </dev/tty # Default? if [ -z "$REPLY" ]; then -
d13r revised this gist
Mar 25, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,8 @@ ask() { default= fi # Ask the question - use /dev/tty in case stdin is redirected from somewhere else read -p "$1 [$prompt] " REPLY </dev/tty # Default? if [ -z "$REPLY" ]; then -
d13r revised this gist
Aug 22, 2014 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,8 @@ # with or without a default answer. It keeps repeating the question until it # gets a valid answer. ask() { # http://djm.me/ask while true; do if [ "${2:-}" = "Y" ]; then -
d13r revised this gist
Aug 19, 2014 . 3 changed files with 51 additions and 42 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,12 @@ # This is a general-purpose function to ask Yes/No questions in Bash, either # with or without a default answer. It keeps repeating the question until it # gets a valid answer. # http://djm.me/ask ask() { while true; do if [ "${2:-}" = "Y" ]; then prompt="Y/n" default=Y @@ -11,20 +17,58 @@ function ask { prompt="y/n" default= fi # Ask the question read -p "$1 [$prompt] " REPLY # Default? if [ -z "$REPLY" ]; then REPLY=$default fi # Check if the reply is valid case "$REPLY" in Y*|y*) return 0 ;; N*|n*) return 1 ;; esac done } # EXAMPLE USAGE: if ask "Do you want to do such-and-such?"; then echo "Yes" else echo "No" fi # Default to Yes if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" Y; then echo "Yes" else echo "No" fi # Default to No if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" N; then echo "Yes" else echo "No" fi # Only do something if you say Yes if ask "Do you want to do such-and-such?"; then said_yes fi # Only do something if you say No if ! ask "Do you want to do such-and-such?"; then said_no fi # Or if you prefer the shorter version: ask "Do you want to do such-and-such?" && said_yes ask "Do you want to do such-and-such?" || said_no 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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +0,0 @@ -
d13r revised this gist
Jul 15, 2012 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,13 @@ else echo "No" fi # Default to Yes if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" Y; then echo "Yes" else echo "No" fi # Default to No if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" N; then echo "Yes" -
d13r revised this gist
Mar 3, 2012 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,10 @@ if ask "Do you want to do such-and-such?"; then echo "Yes" else echo "No" fi # Default to No if the user presses enter without giving an answer: if ask "Do you want to do such-and-such?" N; then echo "Yes" else @@ -22,7 +21,7 @@ if ! ask "Do you want to do such-and-such?"; then said_no fi # Or if you prefer the shorter version: ask "Do you want to do such-and-such?" && said_yes ask "Do you want to do such-and-such?" || said_no -
d13r created this gist
Mar 3, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ This is a general-purpose function to ask Yes/No questions in Bash, either with or without a default answer. It keeps repeating the question until it gets a valid answer. 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ function ask { while true; do if [ "${2:-}" = "Y" ]; then prompt="Y/n" default=Y elif [ "${2:-}" = "N" ]; then prompt="y/N" default=N else prompt="y/n" default= fi # Ask the question read -p "$1 [$prompt] " REPLY # Default? if [ -z "$REPLY" ]; then REPLY=$default fi # Check if the reply is valid case "$REPLY" in Y*|y*) return 0 ;; N*|n*) return 1 ;; esac done } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ # No default answer: if ask "Do you want to do such-and-such?"; then echo "Yes" else echo "No" fi # Default to No: if ask "Do you want to do such-and-such?" N; then echo "Yes" else echo "No" fi # Only do something if you say Yes if ask "Do you want to do such-and-such?"; then said_yes fi # Only do something if you say No if ! ask "Do you want to do such-and-such?"; then said_no fi # Or if you prefer the shorter syntax: ask "Do you want to do such-and-such?" && said_yes ask "Do you want to do such-and-such?" || said_no