Created
April 25, 2019 14:49
-
-
Save Lance-DC/0a378e1274c7e629c3b1feda26278412 to your computer and use it in GitHub Desktop.
Check user input for single valid email address
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 | |
| # Validates user input for a single valid email address | |
| echo -n "Please enter your email address: " | |
| read -r | |
| # use regex to find valid email address formats and store in new variable | |
| userinput=$(echo "$REPLY" | grep -Eo "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b") | |
| # check for no results from grep | |
| if [[ -z "$userinput" ]] | |
| then | |
| echo "Error 100: Not a valid email address." 1>&2 | |
| exit 100 | |
| # check for more than one email address entered | |
| elif [[ $(echo "$userinput" | wc -w) -gt 1 ]] | |
| then | |
| echo "Error 110: Only one email address permitted." 1>&2 | |
| echo "You entered: " 1>&2 | |
| for word in $userinput | |
| do echo "$word" 1>&2 | |
| done | |
| exit 110 | |
| # if checks pass, show email address | |
| else | |
| emailaddr="$userinput" | |
| echo "The email address you entered was: $emailaddr" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment