Sunday, 5 June 2016

How to validate Fully Qualified Domain with bash script?

#!/bin/bash

fqdn="www.google.com"

#FQDN Validation
{
        echo $fqdn | grep -P "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"
} && {
        echo "valid FQDN"  
} || {
        echo "Invalid FQDN Address"
exit 1
}


How to validate the email with bash script?


If else structure created with shell script operators

{
// shell script snippet A
} && {
// shell script snippet B
} || {
//shell script snippet C
}

snippet A executes, B will execute upon successful execution of script A & C will execute upon unsuccessful execution of script A.

grep is a linux command based tool to search the string match pattern' s where -E option for extended regular expression evaluation.



#!/bin/bash
email="test@gmail.com"

#Email Validation
{
echo  | grep -E "^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
} && {
echo "Valid E-Mail Address"
} || {
echo "Invalid E-Mail Address"
exit 1
}