Functions to perform validation on given data.
Validate whether a given input is a valid email address or not.
test='test@gmail.com'
validation::email "${test}"
echo $?
#Output
0
Validate whether a given input is a valid IP V4 address.
ips='
4.2.2.2
a.b.c.d
192.168.1.1
0.0.0.0
255.255.255.255
255.255.255.256
192.168.0.1
192.168.0
1234.123.123.123
0.192.168.1
'
for ip in $ips; do
if validation::ipv4 $ip; then stat='good'; else stat='bad'; fi
printf "%-20s: %s\n" "$ip" "$stat"
done
#Output
4.2.2.2 : good
a.b.c.d : bad
192.168.1.1 : good
0.0.0.0 : good
255.255.255.255 : good
255.255.255.256 : bad
192.168.0.1 : good
192.168.0 : bad
1234.123.123.123 : bad
0.192.168.1 : good
Validate whether a given input is a valid IP V6 address.
ips='
2001:db8:85a3:8d3:1319:8a2e:370:7348
fe80::1ff:fe23:4567:890a
fe80::1ff:fe23:4567:890a%eth2
2001:0db8:85a3:0000:0000:8a2e:0370:7334:foo:bar
fezy::1ff:fe23:4567:890a
::
2001:db8::
'
for ip in $ips; do
if validation::ipv6 $ip; then stat='good'; else stat='bad'; fi
printf "%-50s= %s\n" "$ip" "$stat"
done
#Output
2001:db8:85a3:8d3:1319:8a2e:370:7348 = good
fe80::1ff:fe23:4567:890a = good
fe80::1ff:fe23:4567:890a%eth2 = good
2001:0db8:85a3:0000:0000:8a2e:0370:7334:foo:bar = bad
fezy::1ff:fe23:4567:890a = bad
:: = good
2001:db8:: = good
Validate if given variable is entirely alphabetic characters.
test='abcABC'
validation::alpha "${test}"
echo $?
#Output
0
Check if given variable contains only alpha-numeric characters.
test='abc123'
validation::alpha_num "${test}"
echo $?
#Output
0
Validate if given variable contains only alpha-numeric characters, as well as dashes and underscores.
test='abc-ABC_cD'
validation::alpha_dash "${test}"
echo $?
#Output
0
Compares version numbers and provides return based on whether the value in equal, less than or greater.
test='abc-ABC_cD'
validation::version_comparison "12.0.1" "12.0.1"
echo $?
#Output
0