Hello All,
i am facing difficulty in validating the values,
kindly help me in resolving the issue.
Thanks a lot in advance.
-Chandra
Script:Test.sh
#! /bin/sh
# ***************************************************************************
# Function to display help
function usage()
{
echo ""
echo " Script to transfer data from Source Site"
echo " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
echo ""
echo " -h -help Display Help"
echo " -site=Target Site Name Target Site Name[comma seperated values is allowed]"
echo " -file=File Path File Contains the List which needs to be shared to Target Site"
echo " -include=True/true Argument has to be used in conjuction with -Item_Id & -Rev_Id"
echo " -id=Id Id Which needs to be transfered with Include Switch"
echo " -rev= Revision Id Rev of the Id"
echo ""
echo " Example:1"
echo " Test.sh -site=ABCD -file=./ItemRev_list.txt"
echo ""
echo " Example:2"
echo " Test.sh -site=XYZ -include=True -id=123 -rev=A"
echo ""
echo " Example:3"
echo " Test.sh -site=ABCD,XYZ -include=True -id=123 -rev=A"
echo ""
echo " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
echo ""
}
# **********************************************************
if [ $# -eq 0 ]; then
echo ""
echo "No Parameter provided"
usage
exit 1
fi
# Parse command line options
for PARAM; do
if [[ "$PARAM" = -h* ]]; then
usage
echo ""
exit 0
fi
if [[ "$PARAM" = -include=* ]]; then
INCLUDE=$(echo $PARAM | cut -d"=" -f2)
echo "INCLUDE : $INCLUDE"
fi
if [[ "$PARAM" = -id=* ]]; then
ITEM_ID=$(echo $PARAM | cut -d"=" -f2)
echo "ITEM_ID : $ITEM_ID"
fi
if [[ "$PARAM" = -rev=* ]]; then
REV_ID=$(echo $PARAM | cut -d"=" -f2)
echo "REV_ID : $REV_ID"
fi
if [[ "$PARAM" = -site=* ]]; then
SITE=$(echo $PARAM | cut -d"=" -f2)
echo "SITE : $SITE"
fi
if [[ "$PARAM" = -file=* ]]; then
FILE=$(echo $PARAM | cut -d"=" -f2)
echo "FILE : $FILE"
fi
done
#include parameter value validation
if [[ ( "$INCLUDE" -ne "True" ) || ( "$INCLUDE" -ne "true" ) ]]; then
echo "include will accept only True or true values ..."
echo ""
usage
exit 0
fi
#checking if include is mentioned along with file option
#include should not be used with file option
if [[ ( "$FILE" != "" ) && ( "$INCLUDE" -eq "true" ) ]]; then
echo "File option is not allowed with Include Switch 1"
echo ""
usage
exit 0
elif [[ ( "$FILE" != "" ) && ( "$INCLUDE" -eq "True" ) ]]; then
echo "File option is not allowed with Include Switch 2"
echo ""
usage
exit 0
fi
#file should be existing with non zero size
if [[ "$FILE" != "" ]]; then
if [ -e $FILE -a -s $FILE ]; then
echo ""
else
echo "$FILE is not existing or empty file"
echo ""
exit 0
fi
fi
#include should be used along with item_id & rev_id parameters
if [[ ( "$INCLUDE" -eq "True" ) || ( "$INCLUDE" -eq "true" ) ]]; then
if [[ ( "$ITEM_ID" == "" ) || ( "$REV_ID" == "" ) ]]; then
echo "With include option, item_id & rev_id parameters has to be supplied..."
usage
exit 0
fi
fi
if [[ ( "$INCLUDE" -eq "True" ) || ( "$INCLUDE" -eq "true" ) ]]; then
echo "include switch provided..."
echo "....."
elif [ "$FILE" != "" ]; then
echo "File switch provided..."
echo "....."
else
echo "The Script is not provided with the required arguments"
echo "The below script help information explains about the required parameters..."
echo ""
usage
exit 0
fi
# EOF
exit 0
Requirement:The below examples only should work, other case it should exit the script
Example:1
Test.sh -site=ABCD -file=./ItemRev_list.txt
Example:2
Test.sh -site=XYZ -include=True -id=123 -rev=A
Example:3
Test.sh -site=ABCD,XYZ -include=True -id=123 -rev=A
Example:4
Test.sh -site=ABCD,XYZ -file=./ItemRev_list.txt