Skip to content

Instantly share code, notes, and snippets.

@NickHollow
Created January 1, 2021 05:19
Show Gist options
  • Select an option

  • Save NickHollow/107fd4f53eadd361d8b26b155278edd5 to your computer and use it in GitHub Desktop.

Select an option

Save NickHollow/107fd4f53eadd361d8b26b155278edd5 to your computer and use it in GitHub Desktop.

Revisions

  1. @d13r d13r revised this gist Nov 23, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ask.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # gets a valid answer.

    ask() {
    # https://gist.github.com/davejamesmiller/1965569
    # https://djm.me/ask
    local prompt default reply

    if [ "${2:-}" = "Y" ]; then
  2. @d13r d13r revised this gist Sep 14, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ask.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # gets a valid answer.

    ask() {
    # https://djm.me/ask
    # https://gist.github.com/davejamesmiller/1965569
    local prompt default reply

    if [ "${2:-}" = "Y" ]; then
  3. @d13r d13r revised this gist Sep 14, 2018. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions ask.sh
    Original file line number Diff line number Diff line change
    @@ -6,18 +6,18 @@ ask() {
    # https://djm.me/ask
    local prompt default reply

    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

    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] "
  4. @d13r d13r revised this gist Aug 11, 2018. 2 changed files with 37 additions and 39 deletions.
    40 changes: 1 addition & 39 deletions ask.sh
    Original file line number Diff line number Diff line change
    @@ -37,42 +37,4 @@ ask() {
    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
    }
    36 changes: 36 additions & 0 deletions examples.sh
    Original 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
  5. @d13r d13r revised this gist Oct 10, 2017. 2 changed files with 5 additions and 6 deletions.
    1 change: 0 additions & 1 deletion _README.md
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    The latest version of this function can be found [on my website](https://djm.me/ask).
    10 changes: 5 additions & 5 deletions ask.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    ask() {
    # https://djm.me/ask
    local prompt default REPLY
    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
    read reply </dev/tty

    # Default?
    if [ -z "$REPLY" ]; then
    REPLY=$default
    if [ -z "$reply" ]; then
    reply=$default
    fi

    # Check if the reply is valid
    case "$REPLY" in
    case "$reply" in
    Y*|y*) return 0 ;;
    N*|n*) return 1 ;;
    esac
  6. @d13r d13r revised this gist May 28, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions _README.md
    Original 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).
  7. @d13r d13r revised this gist May 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ask.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # gets a valid answer.

    ask() {
    # http://djm.me/ask
    # https://djm.me/ask
    local prompt default REPLY

    while true; do
  8. @d13r d13r revised this gist Aug 3, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ask.sh
    Original 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
  9. @d13r d13r revised this gist May 6, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions ask.sh
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,11 @@ ask() {
    default=
    fi

    # Ask the question - use /dev/tty in case stdin is redirected from somewhere else
    read -p "$1 [$prompt] " REPLY </dev/tty
    # 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
  10. @d13r d13r revised this gist Mar 25, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ask.sh
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@ ask() {
    default=
    fi

    # Ask the question
    read -p "$1 [$prompt] " REPLY
    # 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
  11. @d13r d13r revised this gist Aug 22, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ask.sh
    Original 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.

    # http://djm.me/ask

    ask() {
    # http://djm.me/ask
    while true; do

    if [ "${2:-}" = "Y" ]; then
  12. @d13r d13r revised this gist Aug 19, 2014. 3 changed files with 51 additions and 42 deletions.
    1 change: 0 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    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.
    58 changes: 51 additions & 7 deletions ask.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,12 @@
    function ask {
    # 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
    34 changes: 0 additions & 34 deletions usage-samples.sh
    Original file line number Diff line number Diff line change
    @@ -1,34 +0,0 @@
    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
  13. @d13r d13r revised this gist Jul 15, 2012. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions usage-samples.sh
    Original 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"
  14. @d13r d13r revised this gist Mar 3, 2012. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions usage-samples.sh
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@
    # No default answer:
    if ask "Do you want to do such-and-such?"; then
    echo "Yes"
    else
    echo "No"
    fi

    # Default to No:
    # 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 syntax:
    # 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
  15. @d13r d13r created this gist Mar 3, 2012.
    1 change: 1 addition & 0 deletions README.md
    Original 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.
    30 changes: 30 additions & 0 deletions ask.sh
    Original 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
    }
    28 changes: 28 additions & 0 deletions usage-samples.sh
    Original 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