William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

bash_cgi.sh

#!/bin/bash

# append: [`date '+%a %b %d %H:%M:%S'`] $*
function timeStamp {
        echo "[`date '+%a %b %d %H:%M:%S'`] $*" >>/home/fgc/log/${CMD}.log
}

function init {
	# Input file includes 4 columns: ROUTE_DT, ROUTE_NBR, AWB, CLASSIFICATION
	# where 2 ACCS tables involved.
	# 1. accs_core_shipment_ref: ROUTE_NBR, ROUTE_DT, CLASSIFICATION
	# 2. shipment: AWB

	. /home/fgc/bin/common/initOracle

	# Bug here: to remove ^M first.
	dos2unix ${eb_file} 2>/dev/null
	if [ ! -s "$eb_file" ]; then
		echo "<h3>The file <$eb_file> you uploaded is invalid! </h3><br>Processing is failure. <br>Please make sure you have a right file to compare."
		echo "<p align=center><input name='TryAgain' value='Try Again' type='button' onClick='javascript:history.go(-1);'></p>"
		exit 4;
	fi

	# eb_file: /home/fgc/etc/tmp/BEB_20070816084223421_TH.txt
	# trim_file: BEB_20070816084223421_TH.txt
	# trim_dir: /home/fgc/etc/tmp/
	trim_file=`echo $eb_file | sed -e "s/.*\///"`
	trim_dir=${eb_file%${trim_file}}

	# ref_ctry=`head -1 $eb_file | cut -f3 -d, | tr "[a-z]" "[A-Z]"`
	ref_ctry=`echo $trim_file | cut -f1 -d"." | sed -e "s/.*_//"`

	if [ -z "$ref_ctry" ]; then
		ref_ctry=TH	#hope not to come here.
	fi

	set `head -2 $eb_file | tail -1 | sed 's/,/ /g'`
	ROUTEDT=$1
	ROUTE=$2

	fgc_file=${eb_file}_fgc
	UNMATCH=${eb_file}_Unmatch
	[ -s $UNMATCH ] && cat /dev/null >$UNMATCH
	NOMATCH=${eb_file}_NoMatch
	[ -s $NOMATCH ] && cat /dev/null >$NOMATCH

	timeStamp "Dir: <$trim_dir>; File: <$trim_file>"
	timeStamp "ROUTE_DATE: <$ROUTEDT>, ROUTE_NBR: <$ROUTE>, REF_COUNTRY: $ref_ctry"
}

function remove_files {
	# keep EB files for track.
	rm -f $fgc_file
	rm -f $UNMATCH
}

# Input: eb_file
# Output: fgc_awk_classcode_file
function get_fgc_data {
	tmp_file=/tmp/$RANDOM_$$
	sqlplus -S $ORAUSR <<! >$tmp_file
	set heading off feedback off term off LINESIZE 32767
	SELECT
		awb_nbr||','||classification
	FROM
		accs.accs_core_shipment_ref acsr,
		ent.shipment ship
	WHERE acsr.route_nbr = '$ROUTE'
	AND trunc(acsr.route_dt) = to_date('$ROUTEDT', 'YYYYMMDD')
	AND ship.shipment_oid_nbr = acsr.shipment_oid
	ORDER BY classification, awb_nbr desc
	/
!
	sort $tmp_file | grep -v "^$" | uniq >$fgc_file
	rm -f $tmp_file
	timeStamp "The <$fgc_file> has total `wc -l $fgc_file | awk '{ print $1 }'` lines."
}

function fgc_awb_classcode {

	match_count=0
	code_count=0
	fgc_count=0
	for i in `cat $fgc_file | grep -v "^$"`
	do
		fgc_awb=`echo $i | cut -f1, -d,`
		fgc_code=`echo $i | cut -f2, -d,`
		#timeStamp "AWB=[$fgc_awb], CLASSFICATION=[$fgc_code]"

		grep $fgc_awb $eb_file >/dev/null
		if [ $? -eq 0 ]; then
			beb_code=$(grep $fgc_awb $eb_file | cut -f4 -d,)
			if [ X"${fgc_code}" = X"${beb_code}" ]; then
				match_count=`expr $match_count + 1`
			else
				code_count=$(( code_count + 1 ))
				echo -e "|\t$fgc_awb,$fgc_code\t\t\t$fgc_awb,$beb_code" >>$UNMATCH
			fi
		else 
			fgc_count=`expr $fgc_count + 1`
			# timeStamp "fgc_count is <$fgc_count>"
			echo -e "<\t$fgc_awb,$fgc_code" >>$UNMATCH
		fi
	done

	timeStamp "(=) Same in FGC and EB:        <$match_count> records. "
	timeStamp "(+) Existing in FGC DB   Only: <$fgc_count> records. See <$UNMATCH> file for details."
	timeStamp "(|) Same AWB, diff CLASS_CODE: <$code_count> records. See <$UNMATCH> file for details."
}

function beb_awb_classcode {
	timeStamp "Inside compare_awb_classcode: <$eb_file>"
	beb_count=0

	# Now process EB file
	for i in `cat $eb_file | grep -v START | grep -v END | grep -v "^$"`
	do
		beb_awb=`echo $i | cut -f3, -d,`
		beb_code=`echo $i | cut -f4, -d,`
		grep $beb_awb $fgc_file >/dev/null
		if [ $? -eq 1 ]; then
			beb_count=$(( beb_count + 1 ))
			echo -e ">\t\t\t\t\t$beb_awb,$beb_code" >>$UNMATCH
		fi
	done
	timeStamp "(-) existing in EB file Only: <$beb_count> records. See <$UNMATCH> file for details."
}

function class_summary {
ed - $NOMATCH << EOF
0a
(=) Same in FGC and EB:        <$match_count> records.
(+) Existing in FGC DB   Only: <$fgc_count> records. 
(|) Same AWB, diff CLASS_CODE: <$code_count> records. 
(-) Existing in EB file Only: <$beb_count> records. 
.
w
q
EOF
}


function display {

cat <<__EOF__
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> FGC EB Records Calculate</title>
<style type="text/css">
	a:link {color:#993300; text-decoration:none}
	a:visited {color:#993300; text-decoration:none}
	a:active {color:#ffffff; background-color:#993300; text-decoration:none}
	a:hover {text-decoration:underline}
</style>
<script type="text/javascript" language="javascript">
        var XMLHttpRequestObject = false;

        if (window.XMLHttpRequest) {
                XMLHttpRequestObject = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
                XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }

        function getData(awb_nbr, divID, ref_ctry) {
		//alert(document.getElementById(divID).style.display);
		if(document.getElementById(divID).style.display == "inline") {
			document.getElementById(divID).style.display = "none";
			return false;
		}

                if(XMLHttpRequestObject) {
                        var obj = document.getElementById(divID);
                        var url = "display_classification.cgi?awb=" + escape(awb_nbr) + "&ref_ctry=" + escape(ref_ctry);
                        XMLHttpRequestObject.open("GET", url, true);

                        XMLHttpRequestObject.onreadystatechange = function() {
                                if (XMLHttpRequestObject.readyState == 4 &&
                                XMLHttpRequestObject.status == 200) {
                                        obj.style.display = "inline";
                                        obj.innerHTML = XMLHttpRequestObject.responseText;
					obj.focus();
                                }
                        }

                        XMLHttpRequestObject.send(null);
                }
        }

        function hideForm(divID) {
		var obj1 = document.getElementById(divID);
		obj1.style.display = "none";
        }

</script>
</head>
<body>
<h1 align="center"><font color="#FF0000">Comparation of FGC and EB</font> </h1>
<a name="head"></a>
<table width="700" border="1" cellspacing="0" cellpadding="0" align="center">
__EOF__


#./display_classification.cgi?awb="substr($2,0,12)">"
# awk -v sq="'" 'BEGIN { print "Here is a single quote <" sq ">" }'
# improvement: use awb=substr($2,0,12), code=substr($2,13) etc instead.
awk -vrefctry=$ref_ctry '
BEGIN {
	FS="\t+"
	fgc_count = 0
	fgc_total = 0
	diff_count = 0
	diff_total = 0
	eb_count = 0
	eb_total = 0
}

/^\(=)/ {
	print "<tr>\n<td bgcolor=\"#99FFCC\"><font color=\"#FF0000\">  <strong>"substr($0, 5)"</strong></font></td>\n</tr>\n";
}

/^\(\+)/ {
	fgc_total = substr($0,index($0, "<")+1,(index($0,">")-index($0,"<")-1));
	print "<tr>\n<td bgcolor=\"#FFFFCC\"><font color=\"#FF0000\">  <strong><a href=\"#fgc\">"substr($0, 5)"</a></strong></font></td>\n</tr>\n";
}

/^\(-)/ {
	eb_total = substr($0,index($0, "<")+1,(index($0,">")-index($0,"<")-1));
	print "<tr>\n<td bgcolor=\"#99FFFF\"><font color=\"#FF0000\">  <strong><a href=\"#eb\">"substr($0, 5)"</a></strong></font></td>\n</tr>\n";
}

/^\(\|)/ {
	diff_total = substr($0,index($0, "<")+1,(index($0,">")-index($0,"<")-1));
	print "<tr>\n<td bgcolor=\"#FFCCFF\"><font color=\"#FF0000\">  <strong><a href=\"#diff\">"substr($0, 5)"</a></strong></font></td>\n</tr>\n";
}

/^</ {
	awb = substr($2, 0, 12);
	code = substr($2,13);

	if (fgc_count == 0) {
		print "<tr align=center bgcolor=#6633CC><th><a name=\"fgc\"></a><font color=white><a href=\"#head\">FGC Only</a></font></th></tr>";
		fgc_count ++;
	}

	print "<tr align=\"center\"> \
<td bgcolor=\"#FFFFCC\"> <a href=\"javascript:void(0);\" onClick=\"getData('\''" awb "'\'', '\''divAWB"awb"'\'', '\''" refctry "'\'')\">" awb "</a>" code "</td>\n\
</tr>\n\
<tr>\n\
<td bgcolor=\"#FFFFCC\"><span style=\"display:none;\" id=\"divAWB"awb"\"></span></td></tr>\n\
</tr>\n"
}

/^\|/ {
	awb1 = substr($2, 0, 12);
	code1 = substr($2,13);
	code3 = substr($3,13);

	if (diff_count == 0) {
		print "<tr align=center bgcolor=#6633CC><th><a name=\"diff\"></a><font color=white><a href=\"#head\">Different Classification</a></font></th></tr>"
		diff_count ++;
	}

	print "<tr align=\"center\"> \
<td bgcolor=\"#FFCCFF\"> <a href=\"javascript:void(0);\" onClick=\"getData('\''" awb1 "'\'', '\''divAWB"awb1"'\'', '\''" refctry "'\'')\">" awb1 "</a>" code1 code3"</td>\n\
</tr>\n\
<tr>\n\
<td bgcolor=\"#FFCCFF\"><span id=\"divAWB"awb1"\" style=\"display:none;\"></span></td>\n\
</tr>\n"
}

/^>/ {
	awb2 = substr($2, 0, 12);
	code2 = substr($2,13);

	if (eb_count == 0) {
		print "<tr align=center bgcolor=#6633CC><th><a name=\"eb\"></a><font color=white><a href=\"#head\">EB Only</a></font></th></tr>"
		eb_count ++;
	}

	print "<tr align=\"center\"> \
<td bgcolor=\"#99FFFF\"> <a href=\"javascript:void(0);\" onClick=\"getData('\''" awb2 "'\'', '\''divAWB"awb2"'\'', '\''" refctry "'\'')\")>" awb2 "</a>" code2 "</td>\n\
</tr>\n\
<tr>\n\
<td bgcolor=\"#99FFFF\"><span id=\"divAWB"awb2"\" style=\"display:none;\"></span></td>\n\
</tr>\n"
}

END {
	printf("<tr align=\"center\"> \
	<th hight=30>There are total <strong><a href=\"#head\">%d</a></strong> Records.</th>\n\
	</tr>\n", (NR-4));
}
' $1

cat <<__EOF__
</table>
</body>
</html>
__EOF__

}

####################
# Main Processing:
####################

Query=${QUERY_STRING:-$1}
eval ${Query//&/\ }

echo "Content-type: text/html"
echo ""

eb_file=$file

CMD=$(basename $0)
CMD=$(echo $CMD | sed 's/\..*$//')

# Test Only:
# if [ -s "/home/fgc/etc/tmp/BEB_TH.txt_NoMatch" ]; then
#	display "/home/fgc/etc/tmp/BEB_TH.txt_NoMatch" 
#	exit;
#fi

#if [ -s "${eb_file}_NoMatch" ]; then
#	display $file
#	exit;
#fi

timeStamp "============== Start Processing $CMD =============="
init
get_fgc_data
fgc_awb_classcode
beb_awb_classcode

# sort the comparing file.
if [ -s $UNMATCH ]; then
	sort -k1,1 $UNMATCH > $NOMATCH
fi

# remove tempororily files.
remove_files
class_summary
timeStamp "============== End Processing $CMD  =============="

display $NOMATCH
Advertisements

One response to “bash_cgi.sh

  1. powercashadvance.com 10/26/2011 at 6:09 pm

    We so appreciate your website post. There are actually so many methods we could put it to very good use with the help of minimum effort on time and money. Thank you really regarding helping have the post reply many concerns we have got before now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: