Skip to content

Instantly share code, notes, and snippets.

@nyteshade
Created March 26, 2022 05:38
Show Gist options
  • Save nyteshade/1dfd453da3e6be256de7391b3cc84b0c to your computer and use it in GitHub Desktop.
Save nyteshade/1dfd453da3e6be256de7391b3cc84b0c to your computer and use it in GitHub Desktop.
YesNo Shell Command
#!/bin/bash
if [[ "--help" = "$1" ]]; then
printf "Usage: ${0} [Message] [Yes] [No] [Yy]\n"
printf " Message - Defaults to \"Are you sure? \"\n"
printf " (notice the space at the end)\n"
printf " Yes - Positive message defaults to Yes\n"
printf " No - Negative message defaults to No\n"
printf " Yy - Letters to use for possitive message\n\n"
printf " Positive messages exit with 0, Negative\n"
printf " messages exit with 1. Use \$\? to check value\n"
exit 0
fi
read -p "${1-Are you sure? }" -n 1 -r
if [[ $REPLY =~ ^[${4-Yy}]$ ]]; then
printf "\x1b[1D${2-Yes}\n"
exit 0
else
printf "\x1b[1D${3-No}\n"
exit 1
fi
@nyteshade
Copy link
Author

nyteshade commented Mar 26, 2022

The command is configurable for output if you need to ask other binary questions. Simply provide the characters for the first / positive answer at the end. Yy checks for both uppercase and lowercase y. Checking for a positive answer is as simple as

if [[ "${?}" -eq 0 ]]; then
  printf "true\n"
else
  printf "false\n"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment