Results 1 to 1 of 1
Hello,
I'm new to Bash scripting, and I've written a little test script to take and validate user input (3 arguments):
#!/bin/bash
# Input validation test
usage()
{
echo -e ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-03-2011 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 20
Need help with storing argument options
Hello,
I'm new to Bash scripting, and I've written a little test script to take and validate user input (3 arguments):
#!/bin/bash
# Input validation test
usage()
{
echo -e "Usage: $0 [OS] [Subnet Mask] [Start IP]\n
-h, prints command summary\n
[OS] centos, debian, or freebsd\n
[Subnet Mask] use /xx notation\n
[Start IP] xxx.xxx.xxx.xxx
e.g. To bind a /28 range of IPs on CentOS\n
./ipalias.sh centos /28 192.168.1.0"
}
if [ $1 = '-h' ];
then
usage
exit 0
fi
if [ $# != 3 ];
then
usage
exit 1
fi
if [[ ($2 =~ '^\/2[4-9]$') || ($2 = /30) ]];
then
echo "Thanks for using regex!"
snm=$2
echo $snm
else echo "Please enter a valid subnet mask."
exit 1
fi
if [[ ($1 = 'centos') || ($1 = 'debian') || ($1 = 'freebsd') ]];
then
echo "Correct operating system."
rlos=$1
echo $rlos
else
echo -e "Invalid operating system selection.\n"
usage
exit 1
fi
if [[ $3 =~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ]];
then
pos1=`echo $3 | cut -d'.' -f 1`
pos2=`echo $3 | cut -d'.' -f 2`
pos3=`echo $3 | cut -d'.' -f 3`
pos4=`echo $3 | cut -d'.' -f 4`
else echo -e "Invalid IP address format.\n"
usage
exit 1
fi
if [[ ($pos1 -le 255) && ($pos2 -le 255) && ($pos3 -le 255) && ($pos4 -le 255) ]];
then
echo "Valid IP entered!"
rlip=$3
echo $rlip
exit 0
else
echo -e "Please input a valid IP address.\n"
usage
exit 1
fi
What I'd like to know how to do, however, is take one option from $1, $2 and $3 respectively and store it for future purposes. I'd like to validate the option passed to argument, rather than the argument itself.
i.e.
usage()
{
echo -e "Usage: $0 -o [OS] -n [Subnet Mask] -s [Start IP]\n
-h, prints command summary\n
-o, operating system centos, debian, or freebsd\n
-n, subnet mask use /xx notation\n
-s, startip xxx.xxx.xxx.xxx
e.g. To bind a /28 range of IPs on CentOS\n
./ipalias.sh -o centos -n /28 -s 192.168.1.0"
}
I've done some Googling but I've been unable to find any clear cut documentation thus far. I know getopt and getopts can be invoked, I'm just not sure how specifically in this script.Last edited by agntsgotnosecret; 02-03-2011 at 09:15 PM.


Reply With Quote
