Hello Chandan_Bose,
Would like to request you if you are merging our code(provided by anyone in here forum members) into a large code(which we do not know your complete requirement and you haven't mentioned into post too), we could not give explanation for it like why it is not working, until/unless we go through your code completely(I had tested my script provided in pervious posts and is working fine for me). Though I have gone through your script and tried my best to correct it, please try it and let me know how it goes(Though I haven't tested it).
#!/usr/bin/bash
clear
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
if [ $# -lt 2 ] ; then
echo " Incorrect Number of Arguments";
echo " Usage : Main_Script <FROM_DATE> <TO_DATE>";
echo " Example : Main_Script 21-JUL-2015 30-JUL-2015";
exit;
fi;
if [ $# == 2 ];
then
if [[ $1 == [0-3][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9] && $2 == [0-3][0-9]-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9] ]]
then
START_DATE=$1
END_DATE=$2;
awk -vstart_date="$START_DATE" -vend_date="$END_DATE" 'BEGIN{
num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", array," ");
for(i=1;i<=num;i++){
Q[array]=i
};
split(start_date, A,"-");
split(end_date, B,"-");
if(A[3] > B[3]){VAL_YEAR="START date year is greater than END date year."}
if(Q[toupper(A[2])] > Q[toupper(B[2])]){VAL_MONTH="START date month is after than END date month."}
if(A[1] > B[1]){VAL_DATE="START date is after than END date."}
if(A[3] <= B[3] && Q[toupper(A[2])] <= Q[toupper(B[2])]){
if((A[3] == B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] == B[3] && Q[toupper(A[2])] < Q[toupper(B[2])]) || (A[3] < B[3] && Q[toupper(A[2])] < Q[toupper(B[2])])){
echo "REPORT FROM " $1 "TO "$2;
}
else {
print "Date is NOT in proper format because of following values:"
print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
}
}
else {
print "Date is NOT in proper format because of following values:"
print VAL_YEAR ORS VAL_MONTH ORS VAL_DATE
}
} '
else
echo "Incorrect date format";
echo $1;
echo "Usage : DD-MMM-YYYY";
echo " Example : Main_Script 21-JUL-2015 30-JUL-2015";
exit;
fi
else
echo "One of the argument is seems to be missing while running the script."
fi
EDIT: Also adding a code with less if-else
conditions by creating a function
. This is only enhancement of my code in previous post.
START_DATE=$1
END_DATE=$2;
if [[ -n $1 && -n $2 ]]
then
awk -vstart_date="$START_DATE" -vend_date="$END_DATE" '
function check(Q,W,T, VAL)
{
if(Q < W)VAL="START date " T " is less than END date " T
if(Q > W)VAL="START date " T " is greater than END date " T
if(Q == W)VAL="START date " T " is equal to END date " T
return VAL
}
function print1(A1,B1,C1)
{
print A1 ORS B1 ORS C1
}
BEGIN{
num=split("JAN FEB MAR APR MAY JUN JULY AUG SEPT OCT NOV DEC", array," ");
for(i=1;i<=num;i++){
Q[array]=i
};
split(start_date, A,"-");
split(end_date, B,"-");
VAL_YEAR=check(A[3], B[3],"YEAR");
VAL_MONTH=check(Q[toupper(A[2])], Q[toupper(B[2])],"MONTH");
VAL_DATE=check(A[1], B[1],"DATE");
if((A[3] == B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] == B[3] && Q[toupper(A[2])] < Q[toupper(B[2])]) || (A[3] < B[3] && Q[toupper(A[2])] == Q[toupper(B[2])] && A[1] < B[1]) || (A[3] < B[3] && Q[toupper(A[2])] < Q[toupper(B[2])])){
print "start_date " start_date " seems to be less than " end_date " end_date."
}
else {
print1(VAL_YEAR,VAL_MONTH,VAL_DATE);
}
} '
else
echo "One of the argument is seems to be missing while running the script."
fi
After running the script as above following are the different results.
./script.ksh 22-MAY-2015 21-May-2015
Date is NOT in proper format because of following values:
START date YEAR is equal to END date YEAR
START date MONTH is equal to END date MONTH
START date DATE is greater than END date DATE
./script.ksh 21-MAY-2015 21-May-2015
Date is NOT in proper format because of following values:
START date YEAR is equal to END date YEAR
START date MONTH is equal to END date MONTH
START date DATE is equal to END date DATE
./script.ksh 20-MAY-2015 21-May-2015
start_date 20-MAY-2015 seems to be less than 21-May-2015 end_date.
Thanks,
R. Singh