Created
March 26, 2022 05:38
-
-
Save nyteshade/1dfd453da3e6be256de7391b3cc84b0c to your computer and use it in GitHub Desktop.
YesNo Shell Command
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 characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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