Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So, in my script, I have to decide whether one of the parameters is a valid email address. I was trying, but it failed.

if $maddr="^.$*\(@\)\(*\)\(.\)\(??*\)"

then
...

It's meant to mean: at least 1 character followed by @ followed by anything followed by a dot and followed by something, which has at least 2 characters.

Any help is appreciated. (Yes, I'm rookie, I'm learning scripting atm)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

The Unix tool to match a string against a regexp is expr:

if expr "$maddr" : '..*@.*\...' > /dev/null; then...

(note that expr regexps are implicitly anchored at the beginning)

Though in this case, simple shell pattern matching would be enough:

case $maddr in
  ?*@*.??*) ...
esac

Note that some shells like zsh, ksh93 and bash have builtin regexp matching operators as an extension above the standard sh syntax, but the syntax varies slightly across those.

pattern='.@.*\...'
if [[ $maddr =~ $pattern ]]; then...

Should work across all three.

(note that those patterns don't guarantee a valid email address).

share|improve this answer
    
second one somehow doesn't work, but first one did. thanks for your time. –  Wanderer Apr 29 at 13:01
_vaddr() { { cat
    printf %s\\n "$@"
} | grep -o '[^ ]*@[^ ]*' |
    grep -q "$REGEX" && {
        $DO_SUCCESS
        } || $HANDLE_FAILURE
}
share|improve this answer
    
grep tries to match every line of $maddr, not $maddr as a whole. Also note that echo does some transformation on its arguments, and should be avoided for arbitrary data. With GNU grep you could do printf %s "$maddr" | grep -zq "$regex" –  Stéphane Chazelas Apr 29 at 6:36
1  
@StephaneChazelas - thats a very good point about echo - i should have tried a little harder. In any case - im certainly no stranger to printf. grep -z i do not agree with - that is not a very portable solution. How can you have newlines in email addresses anyway? The biggest advantage to the above is you can accept streamed data and act as necessary without it first needed to be shell parsed. Not in its current form of course, but it takes very little work to get there. –  mikeserv Apr 29 at 6:52
    
grep -q on non-text input is not portable either, you need printf '%s\n'. It's about validating input so you can exclude things that are not email addresses (where anything could occur) –  Stéphane Chazelas Apr 29 at 6:54
    
@StephaneChezales The "$REGEX" is just a variable and the short-circuit boolean tests either act or dont act according to their assigned function. Inclusion/Exclusion is of course at the implementer's discretion. –  mikeserv Apr 29 at 7:00
    
What I meant is if $maddr may contain non-email-addresses (which we want to exclude), it may contain newline characters. Your new version does DO_SUCCESS as long as at least one address matches. –  Stéphane Chazelas Apr 29 at 7:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.