Help with bash if statement - Shell Scripting

This is a discussion on Help with bash if statement - Shell Scripting ; 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 ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Help with bash if statement

  1. Help with bash 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:

    echo Enter the users e-mail address
    read ADDRESS

    What i'm finding is that sometimes when the operators run the script they are entereing blank information. How can I put a 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.

    string=@
    if [[ $string != "@" ]] ; then
    echo You have entered an invalid e-mail address!
    exit 1
    else
    do something

  2. Re: Help with bash if statement

    Try this:

    #!/usr/bin/env bash

    echo Enter the users e-mail address
    read ADDRESS

    string=".*@.*.[a-zA-Z]{2,}"
    if [[ -z $ADDRESS ]] || ! [[ $ADDRESS =~ $string ]]
    then
    echo "You have entered an invalid e-mail address!"
    exit 1
    else
    echo "Email = $ADDRESS"
    fi

+ Reply to Thread