Thursday, February 16, 2012

Some Unix date operations

input_date_format="%m/%d/%Y"
target_date_format="%m%d%Y"


Inbdate=`date +"$input_date_format" --date="12/11/2011"`
Incdate=`date +"$input_date_format" --date="10/31/2011"`


bdate=`date +"$target_date_format" -d "$Inbdate"`
cdate=`date +"$target_date_format" -d "$Incdate"`
cdate1=`date +"$target_date_format" -d "$Incdate - 1 day"`

-----------------------------------------------------------------------
Some usage of if with regex
--------------------------------

if [[ "$app_id" = Nitesh* ]]
then
app="NiteshApp"
elif [[ "$app_id" = Kumar* ]]
then
app="Kumarapp"
fi

----------------------------------------------------------------------------
Exception handling
------------------------

a.)
sqlcommand=`echo "insert ignore into impressed_$value ( select * from impressed_""$value""_""$app_id"" )"`
echo "executing : $sqlcommand"
mysql -u$root -p$password -h$db_server -P$db_port -D$db_name -e"$sqlcommand"
if [ $? -ne 0 ]
then
echo "Exiting failed : $sqlcommand"
exit 2;
fi

b.) Debgging
#!/bin/sh -ex

--------------------------------------------------------------------------------
use of '\' to ignore new line
-------------------------------------

sqlcommand=`echo "insert into adx_adenginelog_tmp \
( select \
reqType, sessionID, timestamp, appID, phoneNum, numResults \
from log_$ldate where appid='$app_id' and timestamp >= '$ltimestamp' and timestamp <= '$ltimestamp_eod' );"`
echo "Executing: $sqlcommand"
mysql -u"$db_username" -p"$db_password" -h${db_server} -P${db_port} -D${db_name} -e"$sqlcommand"

No comments:

Post a Comment