Sample parameter validation script.sh

#!/bin/bash


echo "`basename $0` Tool"


[[ $# != 3 ]] && echo "Invalid number of arguments" && echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" && exit 128

#number re
re='^[0-9]+$'

if [[ $1 =~ $re ]] ; then
		PARTNERID=$1
                echo "Partner id set to $1"
        else
        	echo "Invalid param #1 should be numeric" 
        	echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" 
         	exit 128
         	
fi

re='^[a-zA-Z0-9][-a-zA-Z0-9]{0,61}[a-zA-Z0-9]$'
if [[ $2 =~ $re ]] ; then
		USERNAME=$2
                echo "Username set to $2"
        else
        	echo "Invalid param #2 should be alphanumeric" 
        	echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" 
         	exit 128
         	
fi

if [[ $3 =~ $re ]] ; then
		PASSWORD=$3
                echo "Password set to $3"
        else
        	echo "Invalid param #3 should be alphanumeric" 
        	echo "Sample Call : create_ftp.sh <numeric_partner_id> <username_alphanum> <password_alphanum>" 
         	exit 128
         	
fi


 

njoy 😉

Write a Reply or Comment

Your email address will not be published.