Help with IF statement

I have a useradd bash script which requests the user enter an e-mail address for the user being created. This is so the user receives his username/password in an e-mail when his/her account is created.

Currently this part of the code is very simple:

Code:
echo Enter the users e-mail address
read ADDRESS

What I'm finding is that sometimes when the operators run the script they are entering blank information. How can I put an if statement in place that enforces they enter an e-mail address format.

I tried the following code but it doesn't work. The idea was to at least verify they are using the @ symbol.

Code:
string=@
if [[ $string != "@" ]] ; then
echo You have entered an invalid e-mail address!
exit 1
else
do something
 
If you want to keep it really really simple, you can echo the ADDRESS and grep for the existence of '@', or you can delete the '@' using tr -d (tr(1)) and compare the before/after result (if they're identical, there was no '@'). There are more ways of doing that (cut, awk, to name a few).

Code:
if ( echo x${ADDRESS} | grep -q \@ )
then it's ok
etc.

Code:
check=$( echo x${ADDRESS} | tr -d '@' )
if [ x${ADDRESS} != x${check} ]
then it's ok
etc.

The 'x' is to prevent an empty value (i.e. no ADDRESS) from screwing up the script due to operator errors.

Note, this is just very rudimentary, there are more elegant solutions, using e.g. Perl or other tools that are better at comparing and manipulating strings and expressions. If you have to stick to a shell script, consider using /bin/sh instead of /usr/local/bin/bash.
 
$ADDRESS is the string to be compared. [[ is a bashism for [(1).

If you want to check for an empty string, one of the standard ways is
Code:
if [ "$ADDRESS"x = "x" ]; then
  echo "empty address"
fi

If you want to check for the presence of an @ symbol in $ADDRESS, see expr(1). Neither of those tests verify a good email address; both combined is better.
 
And you really, really, really should put your variables inside "", especially when used with [ ]. All it takes is for someone to "accidentally" put a space into a variable, and all your [ ] statements break.
 
The suggestions above tell you how to do rudimentary checks, such as empty addresses or addresses with no @ in them. However, in some cases a local username might also be considered a valid e-mail address. Furthermore, a "fully qualified" e-mail addres not only requires a @, it usually also requires a dot in the domain part (there must be a TLD unless you have a domain called "localdomain" or something). And as it turns out when you read the relevant RFC(s) there are several more restrictions.

Moral of the story: shell scripting is good enough for rudimentary checks, but the more thorough you wish to check the e-mail addresses, the more you'll need to have the checking done by something other than a shell script, such as a Perl script, a C program (possibly augmented with (f)lex) or if you're feeling adventurous even Haskell.

Fonz
 
Appendix B in Jeffrey Friedl's awesome Mastering Regular Expressions has a full regular expression for testing email addresses. With comments removed, it's 6,598 bytes.

Testing email addresses well is tricky.
 
Back
Top