Conditional emailing with mysql query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional emailing with mysql query
# 1  
Old 06-15-2013
Conditional emailing with mysql query

Hi, Im new with bash scripting and I want to create a conditional emailing with bash scripting. But it doesn't workout.

the conditional requirement are; if the TextDecoded value from inbox table are not the same with sms_keyword value from kontak_group table then don't email.

It keep emailing whether it meet or not using the requirements above.

Please help, and thank you very much. Smilie


TABLES:

Code:
mysql -uroot -p123456 smsd -e "SELECT * FROM kontak_group"
+- -------------+----------+-------------+---------------+
| group_name    | group_ID  | sms_keyword | email_group    |
+---------------+----------+-------------+---------------+
| Information     | 1            | IT                | it@acme.com  |
+---------------+----------+-------------+---------------+

Code:
 mysql -uroot -p123456 smsd -e "SELECT * FROM inbox"
+-------------+-------------------+---------------+-------------+----+--------------+-----------+-----------+
| UpdatedInDB | ReceivingDateTime | SenderNumber | TextDecoded | ID  | status_reply | RecipientID | Processed |
+-------------+-------------------+---------------+-------------+----+--------------+-----------+-----------+

# send to email function
Code:
mysql -uroot -p123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox WHERE TextDecoded NOT regexp ( select concat_ws ( '', '^(', group_concat(sms_keyword separator '|'), ').*' ) from kontak_group) ORDER BY ReceivingDateTime DESC LIMIT 1" | mailx -s "$SUBJECT" -a From:$SENDER $EMAIL_ADDRESS

# 2  
Old 06-15-2013
mailx will run as long as there is some output coming thru the pipe regardless of whether or not it is what you expected ... you may want to put the mysql output into a variable, check that variable value and then pipe to mailx if appropriate (code sample below may need your tweaking)...

Code:
TEXTD=$(mysql -uroot -p123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox WHERE TextDecoded NOT regexp ( select concat_ws ( '', '^(', group_concat(sms_keyword separator '|'), ').*' ) from kontak_group) ORDER BY ReceivingDateTime DESC LIMIT 1")
if $TEXTD
then
   echo $TEXTD | mailx -s "$SUBJECT" -a From:$SENDER $EMAIL_ADDRESS
fi


Last edited by Just Ice; 06-15-2013 at 03:39 AM.. Reason: code tags
# 3  
Old 06-15-2013
Hi, thanks for your repply.

I changed the code to this:

Code:
status=0
PTMemails="acme@gmail.com"
HCSemails="acme@gmail.com"
PTMSubject="Email to group status - Sent"
HCSSubject="Email to group status - Sent"
CHECKER='mysql -u root --password=123456 smsd -e "select * from inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w 'ADM|BS|HCS ADM|HC|IRC|IT|PTM|HCS''

if [ "CHECKER" = "TextDecoded: PTM test filter 19:38" ]; then
	message="send email to PTM"
	status=1
fi

if [ "CHECKER" = "TextDecoded: HCS test filter 19:38" ]; then
	message="send email to HCS"
	status=2
fi

if [ $status = 1 ]; then
	for address in $PTMemails; do
		echo -e $message | mail -s $PTMSubject $address
		echo " email sent to PTM contacts"
	done
fi

if [ $status = 2 ]; then
	for address in $HCSemails; do
		echo -e $message | mail -s $HCSSubject $address
		echo " email sent to PTM contacts"
	done
fi

and give me this output:

Code:
+ status=0
+ PTMemails=acme@gmail.com
+ HCSemails=acme@gmail.com
+ PTMSubject=Email to group status - Sent
+ HCSSubject=Email to group status - Sent
+ HCS ADM
kategori.sh: 1: HCS: not found
+ HC
kategori.sh: 1: HC: not found
+ BS
kategori.sh: 1: BS: not found
+ IRC
kategori.sh: 1: IRC: not found
+ IT
kategori.sh: 1: IT: not found
+ CHECKER=mysql -u root --password=gurlia001 smsd -e "select * from inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w ADM
+ PTM
kategori.sh: 1: PTM: not found
+ HCS
: not found: 1: HCS
+
: not found: 1:
kategori.sh: 20: Syntax error: word unexpected (expecting "do")

# 4  
Old 06-15-2013
i'm thinking your mysql command is putting out extraneous info ... see if you can redirect the unneeded info (i.e., CHECKER=$(some_command | some_command 2> /dev/null) ... echo out the value of $CHECKER to make sure are only getting what you need ...

also, check your egrep output and see if HCS or ADM will not show HCS ADM ...

you could also combine your blocks to make the script shorter (see below) ...
Code:
PTMemails="acme@gmail.com"
HCSemails="acme@gmail.com"
PTMSubject="Email to group status - Sent"
HCSSubject="Email to group status - Sent"
CHECKER='mysql -u root --password=123456 smsd -e "select * from inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w 'ADM|BS|HCS ADM|HC|IRC|IT|PTM|HCS''   ### tweak this line

if [ "CHECKER" = "TextDecoded: PTM test filter 19:38" ]; then
	message="send email to PTM"
	for address in $PTMemails; do
	      echo -e $message | mail -s $PTMSubject $address
	done
	echo " email sent to PTM contacts"
fi

if [ "CHECKER" = "TextDecoded: HCS test filter 19:38" ]; then
	message="send email to HCS"
	for address in $HCSemails; do
		echo -e $message | mail -s $HCSSubject $address
	done
	echo " email sent to PTM contacts"
fi

# 5  
Old 06-15-2013
changed and echo from $CHECKER :

Code:
mysql -u root --password=123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w 'ADM|BS|HC|IRC|IT|PTM|HCS'
 TextDecoded: ADM test filter 19:38

The result are right. I only want the newest row from inbox table with TextDecoded value are starting with the word described from egrep.

changed the whole script:

Code:
ADMemails="acme@gmail.com"
HCSemails="acme@gmail.com"
ADMSubject="Email to group status - Sent"
HCSSubject="Email to group status - Sent"
CHECKER='mysql -u root --password=123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w 'ADM|BS|HC|IRC|IT|PTM|HCS''   ### tweak this line
if [ "CHECKER" = "TextDecoded: ADM*" ]; then
	message="send email to ADM"
	for address in $ADMemails; do
	      echo -e $message | mail -s $ADMSubject $address
	done
	echo " email sent to ADM contacts"
fi
if [ "CHECKER" = "TextDecoded: HCS*" ]; then
	message="send email to HCS"
	for address in $HCSemails; do
		echo -e $message | mail -s $HCSSubject $address
	done
	echo " email sent to PTM contacts"
fi

gives an error result:

Code:
+ ADMemails=acme@gmail.com
+ HCSemails=acme@gmail.com
+ ADMSubject=Email to group status - Sent
+ HCSSubject=Email to group status - Sent
+ IRC
kategori2.sh: 1: IRC: not found
+ HC
kategori2.sh: 1: HC: not found
+ IT
kategori2.sh: 1: IT: not found
+ BS
kategori2.sh: 1: BS: not found
+ PTM
kategori2.sh: 1: PTM: not found
+ HCS
kategori2.sh: 1: HCS: not found
+ CHECKER=mysql -u root --password=123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w ADM
kategori2.sh: 8: Syntax error: word unexpected (expecting "do")


Last edited by jazzyzha; 06-15-2013 at 12:42 PM.. Reason: wrong paste script
# 6  
Old 06-15-2013
try this one ... btw, i am testing this on ksh on solaris 10 ... works fine here ...
Code:
ADMemails="acme@gmail.com"
HCSemails="acme@gmail.com"
ADMSubject="Email to group status - Sent"
HCSSubject="Email to group status - Sent"
CHECKER='mysql -u root --password=123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w 'ADM|BS|HC|IRC|IT|PTM|HCS' 2> /dev/null'

echo $CHECKER | grep ADM > /dev/null
if [ $? -eq 0 ]
then
	message="send email to ADM"
	for address in $ADMemails
        do
	      echo -e $message | mail -s $ADMSubject $address
	done
	echo " email sent to ADM contacts"
fi

echo $CHECKER | grep HCS > /dev/null
if [ $? -eq 0 ]
then
	message="send email to HCS"
	for address in $HCSemails
        do
	      echo -e $message | mail -s $HCSSubject $address
	done
	echo " email sent to PTM contacts"
fi

# 7  
Old 06-16-2013
Thanks Bro, but it doesn't work out. I already tried use your script. btw, Im using debian squeeze.

The result are:
Code:
+ ADMemails=acme@gmail.com
+ HCSemails=acme@gmail.com
+ ADMSubject=Email to group status - Sent
+ HCSSubject=Email to group status - Sent
+ IRC
senin.sh: 1: IRC: not found
+ HC
senin.sh: 1: HC: not found
+ IT
senin.sh: 1: IT: not found
+ BS
senin.sh: 1: BS: not found
+ PTM
senin.sh: 1: PTM: not found
+ HCS 2> /dev/null
senin.sh: 1: HCS 2> /dev/null: not found
+ CHECKER=mysql -u root --password=123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w ADM
+ grep ADM
+ echo
+ [ 1 -eq 0 ]
+ grep HCS
+ echo
+ [ 1 -eq 0 ]


still theres no mail receive from it.

Last edited by jazzyzha; 06-16-2013 at 09:52 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Need help in mysql query

Hi All, i have a table in mysql with the following data Table name Test Assettype Serial_No Status location Mouse 123456 In Stock chennai Mouse 98765 Allocated chennai Keyboard ... (2 Replies)
Discussion started by: venkitesh
2 Replies

2. Shell Programming and Scripting

Need help on conditional emailing

Hi All, The following databse table maintains VENDOR and EMAIL details. VENOR_NAME VENDOR_EMAIL DELL surendra@dell.com HP rajkamal@hp.com ACER sumathi@acer.com NOKIA kunal@nokia.com SONY sinu@sony.com We have to find emaild id of a vendor based... (7 Replies)
Discussion started by: ROCK_PLSQL
7 Replies

3. Shell Programming and Scripting

Conditional bash/mysql query help

I think(hope) I've got a simple one - I just need to send an email if a mysql query returns any results (ideally - it will never match). Currently I just pipe the mysql query output to the mail program, but of course that emails regardless of the output( and I check this every 10 minutes from... (5 Replies)
Discussion started by: jcass78
5 Replies

4. Programming

mysql query help

Hello i have created mysql query to compare to values and get difference in percentage as following: SELECT file_name, 100 - ((100 * (SELECT file_count FROM xipi_files z WHERE x.file_group = z.file_group AND x.file_name = z.file_name AND z.insert_date = CURDATE( ) - INTERVAL 1 DAY)) /... (1 Reply)
Discussion started by: mogabr
1 Replies

5. Shell Programming and Scripting

mysql query in shellscript

Hi, I want to access mysql query from database , for that i have tried the below code #! /bin/bash TABLE_NAME=database1 USER_NAME=root IP_ADDR=111.20.9.256 somevar=`echo "select altid from alert where altid='2724'"| mysql -h $IP_ADDR -u $USER_NAME $TABLE_NAME ` echo $somevar ... (1 Reply)
Discussion started by: aish11
1 Replies

6. Web Development

mysql query help

hello all i have 2 columns every column in the following format column1 2011-04-01 11:39:54 column2 2019-02-03 00:00:00 i want get difference between above data as following 2 days 11:39 how to do so ? i tried many functions but nothing works please advice what is the query... (6 Replies)
Discussion started by: mogabr
6 Replies

7. Shell Programming and Scripting

mysql help : query with 2 conditionals

Hi there, I have a table that stores multiple records for many different servers, each of which is timestamped ... I wanted to write a query that would enable me to only output the "latest" record (based on timestamp) for each "unique" server. So for example my main table looks like this ... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

8. Programming

How to query one to many mysql

Hi there, I have a hierarchical database that include 4 tables. Table A is the parent of B, B is Parent of C, C is parent of D. If I want to query everything in D that is associated with A.name, how do I do that? Thanks! YanYan (0 Replies)
Discussion started by: pinkgladiator
0 Replies
Login or Register to Ask a Question