Check Receipt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check Receipt
# 1  
Old 07-04-2012
Check Receipt

Hi Experts,

I am experiencing problems in running the following attached script which is to check received data from user and process it for return delivery.

I encounter the below errors and I humbly request your assistance.


Code:
#FUNCTIONS

InvalidKeyword()
{
	#arg1 must be shortcode, arg2 must be subscriber msisdn
	message="Invalid Keyword, contact us on !!!"
	log "error; subscriber [$getmsisdn] has requested an invalid keyword [$getkeyword]" $log
	$mysqlpath/mysql -u wirels -e  "INSERT INTO dB2.outbox VALUES ('NULL','$1',$2, '$message', '', 'N',NOW())"
	$mysqlpath/mysql -u wirels -e  "UPDATE dB.dB_table set SMS_Sent=1 WHERE sender='$2'"
	#touch tmp invalid keyword file to show sub requested invalid keyword
	touch $tmpdir/$2.keyword.invalid
}

ScheduleSend()
{
	getmsisdn=$1
	getkeyword=$2
	getshortcode=$3
	alloctbl="dB.contentallocation"
	keywordtbl="`grep -i "^$getkeyword" $keywordcfg | awk '{print $2}'`"

	#if keyword does not exist
        if [ -z "$keywordtbl" ];then
		InvalidKeyword $getshortcode $getmsisdn 
                return
 	fi	

	#check if subscriber has entry in contentallocation for their keyword but not in keyword table
	$mysqlpath/mysql -u wirels -e  "SELECT b.ID FROM $keywordtbl b LEFT JOIN $alloctbl t2 ON b.ID = t2.ID and t2.MSIDN='$getmsisdn' and t2.Cont='$getkeyword' where t2.ID IS NULL" | grep -v ID > $tmpdir/alloc.$getmsisdn.tmp
 
	#proceed 
	if [ `cat $tmpdir/alloc.$getmsisdn.tmp | grep [0-9] | wc -l | awk '{print $1}'` -gt 0 ];then
		#set message to send to subscriber
		gettargetdatadir="`grep -i "^$getkeyword" $keywordcfg | awk '{print $3}'`"
		log "info; begin processing for subscriber [$getmsisdn] with keyword [$getkeyword] to shortcode [$getshortcode]" $log
		if [ -d $gettargetdatadir ];then
			#construct path, choose random file to send
		        $mysqlpath/mysql -u wirels -e  "SELECT Description,Folder,Format,ID FROM $keywordtbl WHERE Keyword LIKE '%$getkeyword%' ORDER BY RAND() LIMIT 1" | grep -v Description > $tmpdir/getdata.$getmsisdn.tmp 
			for line in "`cat $tmpdir/getdata.$getmsisdn.tmp`"
			do
				filename="`echo $line | awk '{print $1}'`"
				subdir="`echo $line | awk '{print $2}'`"
				newkeywordID="`echo $line | awk '{print $4}'`"
			done

			if [ ! -f "$gettargetdatadir/$subdir/$filename" ];then
				log "error; unable to find link to send [$gettargetdatadir/$subdir/$filename] for subscriber [$getmsisdn]"
				InvalidKeyword $getshortcode $getmsisdn
				return
			fi
	
			type="`basename $gettargetdatadir`"
			url="`grep -i "^$getkeyword" $keywordcfg | awk '{print $4}'`"
			message="$urldownloadtxt $url/$type/$subdir/$filename"
		else
			#get from db
			$mysqlpath/mysql -u wirels -e  "SELECT ID,Description FROM $keywordtbl WHERE Keyword LIKE '%$getkeyword%' ORDER BY RAND() LIMIT 1" | grep -v Description > $tmpdir/getdata.$getmsisdn.tmp 
			for line in "`cat $tmpdir/getdata.$getmsisdn.tmp`"
			do
				newkeywordID="`echo $line | awk '{print $1}'`"
				message="`echo $line | sed s/"^[0-9]*."/""/g`"
			done
					
		fi	

		if [ "$message" == "" ];then
			InvalidKeyword $getshortcode $getmsisdn
			return
		fi
		
		#encode message string
		#message="`echo $message | sed s/"'"/"\\\'"/g`" 
		echo $message | sed s/"'"/"\\\'"/g > $tmpdir/message.$getmsisdn.tmp
		message="`cat $tmpdir/message.$getmsisdn.tmp`" 
		rm -f $tmpdir/message.$getmsisdn.tmp

		log "info; preparing content [$message] content id [$newkeywordID] for subscriber [$getmsisdn] " $log
		#update contentallocation tbl
		$mysqlpath/mysql -u wirels -e "INSERT INTO $alloctbl VALUES (null,'$newkeywordID','$getmsisdn','$getkeyword','',NOW())"
		#echo $alloctbl VALUES null $newkeywordID $getmsisdn $getkeyword "" now
		log "info; contentallocation tbl updated for subscriber[$getmsisdn]" $log

		#update outbox
		#VALUES NULL, SMS_FROM which is shortcode , SMS_TO which is sub's number, SMS_TEXT which is message , '', 'N', NOW()
		$mysqlpath/mysql -u wirels -e  "INSERT INTO dB2.outbox (id,SMS_FROM,SMS_TO,SMS_TEXT,SMS_UDH,SMS_SENT,SMS_TIME) VALUES ('NULL','$getshortcode','$getmsisdn', '$message','', 'N',NOW())"
		
		#echo INSERT INTO dB2.outbox VALUES NULL $getshortcode $getmsisdn $message 'null' N
		log "info; outbox tbl updated for subscriber[$getmsisdn]" $log
		
		#update inbox
		$mysqlpath/mysql -u wirels -e  "UPDATE dB.dB_table set SMS_Sent=1 WHERE sender='$getmsisdn'"
		log "info; dB_table tbl updated for subscriber[$getmsisdn]" $log
	else
		log "warn; subscriber [$getmsisdn] has requested all their content for keyword [$getkeyword]" $log 
	fi
	return
}

log()
{
	echo "`date '+%Y%m%d %H%M%S'` $1" >> $log
}

i=0

accountcntry="$1" #e.g. drc za

mysqlpath="/usr/bin"

#include variable definitions file
. /home/wirels/checksmsrcvd/env/env.checksmsrcvd.sh

#defaults
log="/home/wirels/checksmsrcvd/$accountcntry/log/keywordrcv.log"
tmpdir="/home/wirels/checksmsrcvd/$accountcntry/tmp"
lockfile="$tmpdir/my.lck"

if [ ! -f $lockfile ];then
	touch $lockfile
else
	log "system; another instance for script already running, will exit" $log
	exit
fi

$mysqlpath/mysql -u wirels -e  "SELECT * FROM dB.dB_table where SMS_Sent = 0" | grep -wv id > $tmpdir/inbox.tmp

if [ "`cat $tmpdir/inbox.tmp | wc -l | awk '{print $1}'`" -eq 0 ];then
	log "info; nothing to process in db_table $log
fi
cat $tmpdir/inbox.tmp  | while read entry
do
        msisdn=`echo $entry | awk '{print $2}'`
        keyword=`echo $entry | awk '{print $4}'`
        shortcode=`echo $entry | awk '{print $3}'`
	subscriptions_count="`grep $accountcntry $subscriptionscfg | egrep -w "\+$shortcode|$shortcode" | awk '{print $3}'`"
	[ -z $subscriptions_count ] && subscriptions_count=0
	subscriptions_period="`grep $accountcntry $subscriptionscfg | egrep -w "\+$shortcode|$shortcode" | awk '{print $4}'`"
        ScheduleSend $msisdn $keyword $shortcode $accountcntry
	#update subscriptions table if subscriber has sent to a repeated content shortcode and keyword valid
	if [ ! -f $tmpdir/$msisdn.keyword.invalid ];then
        	if [ $subscriptions_count -gt 0 ];then
        		$subscriptions_count=`expr $subscriptions_count - 1`
        		$mysqlpath/mysql -u wirels -e  "INSERT INTO dB.subscriptions (msisdn,shortcode,keyword,counts,receivedtime,lastscheduled,period) VALUES ('$getmsisdn','$getshortcode','$getkeyword','$subscriptions_count',NOW(),NOW(),'$subscriptions_period')"
        	fi
	fi
done

#clean up
rm -f $tmpdir/alloc*.tmp $tmpdir/getdata*.tmp $tmpdir/message*.tmp $tmpdir/*keyword.invalid
rm -f $lockfile
exit

Kindly see errors as:

Code:
: command not foundne 1:
: command not foundne 3:
: line 4: syntax error near unexpected token `
: line 4: `InvalidKeyword()

Please help...


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 07-04-2012 at 07:11 AM.. Reason: code tags, see PM
# 2  
Old 07-04-2012
Try executing the script with debugging option by adding set -x on the lines top.
# 3  
Old 07-04-2012
The corrupt error messages, suggest that this script is not a valid script file.
Check whether the script has been edited with a Microsoft editor such as Windows Notepad and then transferred to a unix system without conversion.

To visually check the script for funny characters, use this sed command to make funny characters visible:
Code:
sed -n l scriptname | more

A normal line will show with ending $. A Microsoft line will show with ending \r$.


Also, please post what Operating System and version you are running and what Shell you use.
# 4  
Old 07-04-2012
Yes Methyl,

the
Code:
sed

command revealed \r$
Indeed I did a few editing on notepad with the Windows OS and re-uploaded.
Can I use chmod command to reconvert it? Maybe that should work?

The server hosting the script is a Ubuntu 12 and I am using bash.

Please advise.

Quote:
Originally Posted by methyl
The corrupt error messages, suggest that this script is not a valid script file.
Check whether the script has been edited with a Microsoft editor such as Windows Notepad and then transferred to a unix system without conversion.

To visually check the script for funny characters, use this sed command to make funny characters visible:
Code:
sed -n l scriptname | more

A normal line will show with ending $. A Microsoft line will show with ending \r$.


Also, please post what Operating System and version you are running and what Shell you use.
# 5  
Old 07-04-2012
Quick conversion (delete all the carriage return characters):

Code:
cat oldscriptname | tr -d '\r' > newscriptname   # Remove carriage-returen charactes
chmod 755 newscriptname  # Make corrected script executable
mv oldscriptname oldscriptname.sav # Save the old script in case of accidents
mv newscriptname oldscriptname # Correct the name of the corrected script



If you originally used ftp to transfer the file, make sure that you use ASCII mode next time (not BINARY mode).
# 6  
Old 07-16-2012
worked great!
thank you methyl; I appreciate your help a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

2. Shell Programming and Scripting

check in and check out comments

Hi Do you know how can I get all check in and check out comments for a period of time with Surround SCM CLI ---------- Post updated at 02:00 AM ---------- Previous update was at 01:56 AM ---------- Do you have NAnt tasks that allow us to access Evolution from an NAnt build script? (0 Replies)
Discussion started by: saku
0 Replies

3. Shell Programming and Scripting

scripting mail receipt and saving attachment

I'm working on a script which is supposed to check an email account and automatically download .doc attachments, but I'm having some problems. I'm using Mutt to check the email account (IMAP), and I can manually get the attachment and saving it, but I cannot for the life of me work out how to... (4 Replies)
Discussion started by: spynappels
4 Replies

4. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

5. UNIX for Dummies Questions & Answers

How to get read receipt notification on UNIX

Hi, We are sending mail from UNIX Sendmail utility. Is there any feature through which we can receive read receipt notification for e-mails sent through sendmail? Thanks. (1 Reply)
Discussion started by: DejaVu
1 Replies

6. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies

7. UNIX for Advanced & Expert Users

mail receipt

how can I control a mail receipt sent from unix using sendmail (4 Replies)
Discussion started by: npires
4 Replies

8. Shell Programming and Scripting

how to request a "read" or "delivered" receipt for mails

Dears, I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails. The script send mails on behalve of an specific... (1 Reply)
Discussion started by: plelie2
1 Replies
Login or Register to Ask a Question