Smarter conditional script with Table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Smarter conditional script with Table
# 1  
Old 06-27-2013
Smarter conditional script with Table

Hi, currently I have a script with conditional checking. But it's not flexible to use, because i must create a more conditional script if there are more keyword (see table) required. Question is: Can I create a more smarter script that will be more flexible?
I appreciate any help anyone can give me here to get this to work.
Gracias!

My Table:
Code:
mysql> select * from kontak_group;
+-----------------------------+----------+-------------+-------------------------------+
| group_name                  | group_ID | sms_keyword | email_group                   |
+-----------------------------+----------+-------------+-------------------------------+
| IT Dev                      |        1 | IT          | it.patra@acme.com      |
| IR & Complain               |        2 | IRC         | irc.patra@acme.com     |
| Payroll & Travel Management |        3 | PTM         | ptm.patra@acme.com |
| Adm & Document              |        4 | ADM         | adm.patra@acme.com     |
| Business Support            |        5 | BS          | bs.patra@acme.com      |
+-----------------------------+----------+-------------+-------------------------------+

My script:

Code:
KRITERIA=$(mysql -u root --password=123456 smsd -e "select concat_ws ( '', group_concat(sms_keyword separator '|') ) from kontak_group")

CHECKER=$(mysql -u root --password=123456 smsd -e "SELECT ID, SenderNumber, TextDecoded FROM inbox ORDER BY ReceivingDateTime DESC LIMIT 1  \G" |egrep -w '$KRITERIA' 2> /dev/null)

SPAM=$(mysql -uroot -p123456 smsd -e "INSERT INTO inbox_spam SELECT * FROM inbox WHERE TextDecoded NOT regexp ( select concat_ws ( '', '^(', group_concat(sms_keyword separator '|'), ').*' ) from kontak_group)")

echo $CHECKER | grep ADM > /dev/null
if [ $? -eq 0 ]
then
	message=$message2
	for address in $ADMemails
        do
	      echo $message | mailx -s "$ADMSubject" -a From:$SENDER $EMAIL_ADDRESS
	done
	echo " email sent to ADM contacts"
else
	$SPAM
fi

echo $CHECKER | grep IRC > /dev/null
if [ $? -eq 0 ]
then
	message=$message2
	for address in $HCSemails
        do
	      echo $message | mailx -s "$IRCSubject" -a From:$SENDER $EMAIL_ADDRESS
	done
	echo " email sent to IRC contacts"
else
	$SPAM
fi

echo $CHECKER | grep PTM > /dev/null
if [ $? -eq 0 ]
then
	message=$message2
	for address in $PTMemails
        do
	      echo $message | mailx -s "$PTMSubject" -a From:$SENDER $EMAIL_ADDRESS
	done
	echo " email sent to PTM contacts"
else
	$SPAM
fi

echo $CHECKER | grep BS > /dev/null
if [ $? -eq 0 ]
then
	message=$message2
	for address in $BSemails
        do
	      echo $message | mailx -s "$BSSubject" -a From:$SENDER $EMAIL_ADDRESS
	done
	echo " email sent to BS contacts"
else
	$SPAM
fi


Last edited by jazzyzha; 06-27-2013 at 12:15 AM..
# 2  
Old 06-27-2013
you can use case ... sample below assumes only one group notified per run - remove break in case statement if not required ...

file.txt (created manually or from mysql command)
Code:
IRC
PTM
ADM
BS

script
Code:
CNT=0    # use to check if any of the group errors are found
for GRP in $(< file.txt)
do
     echo $CHECKER | grep $GRP > /dev/null
     if [ $? -eq 0 ]
     then
           CNT=$(($CNT + 1))
           case $GRP in 
           ADM)message=$message2;;
           IRC) message=$message2;;
	   PTM) message=$message2;;
            BS) message=$message2;;
           esac
           eval SUBJECT=\$${GRP}subject
           eval ADDLIST=\$${GRP}emails
           for EMAIL_ADDRESS in $ADDLIST
           do
	        echo $message | mailx -s "$SUBJECT" -a From:$SENDER $EMAIL_ADDRESS
	   done
           echo " email sent to $GRP contacts"
           break
    fi
done

if [ $CNT -eq 0 ]
then
      $SPAM
fi


Last edited by Just Ice; 06-27-2013 at 04:25 AM.. Reason: fixed break location
# 3  
Old 06-27-2013
thank you brother.

but what i'm saying is, can i run a script that reads my table row like this:

if message contain a word from one of sms_keyword field then email it to the corresponden row from email group.

example:

if word ADM found then email to adm.patra@acme.com reading from the table without defining the keyword on the scripts or file.
# 4  
Old 06-27-2013
change the lines below ...
Code:
           eval SUBJECT=\$${GRP}subject
           eval ADDLIST=\$${GRP}emails

to
Code:
           SUBJECT=$mysql_code_to_grab_GRP_email_subject
           ADDLIST=$mysql_code_to_grab_GRP_email_address_list

# 5  
Old 06-27-2013
Do you mean like this?

Code:
GRP_email_subject=$(mysql -uroot -p123456 smsd -e "SELECT email_subject FROM kontak_group")
GRP_email_address_list=$(mysql -uroot -p123456 smsd -e "SELECT email_group FROM kontak_group")

CNT=0    # use to check if any of the group errors are found
for GRP in $(< file.txt)
do
     echo $CHECKER | grep $GRP > /dev/null
     if [ $? -eq 0 ]
     then
           CNT=$(($CNT + 1))
           case $GRP in 
           ADM)message=$message2;;
           IRC) message=$message2;;
	   PTM) message=$message2;;
            BS) message=$message2;;
           esac
#          eval SUBJECT=\$${GRP}subject
	   SUBJECT=$GRP_email_subject
#          eval ADDLIST=\$${GRP}emails
	   ADDLIST=$GRP_email_address_list
           for EMAIL_ADDRESS in $ADDLIST
           do
	        echo $message | mailx -s "$SUBJECT" -a From:$SENDER $EMAIL_ADDRESS
	   done
           echo " email sent to $GRP contacts"
           break
    fi
done

if [ $CNT -eq 0 ]
then
      $SPAM
fi

But still the script using this:
Code:
          ADM)message=$message2;;
           IRC) message=$message2;;
	   PTM) message=$message2;;
            BS) message=$message2;;

and still using .txt files.

Smilie
# 6  
Old 06-27-2013
you can create file.txt automatically by running another mysql command against the kontack_group table on the start of the script ... then $message2 can also be added into another mysql table and correlated with the sms_keyword so you can grap them automatically from your script ...
Code:
$(mysql_code_to_grab_sms_keywords) > file.txt

...
...
      message="$(mysql_code_to_grab_message_for_GRP)"
...
...

# 7  
Old 06-28-2013
is this right? i changed to:

Code:
CNT=0    # use to check if any of the group errors are found
for GRP in $((mysql --skip-column-names -uroot -p123456 smsd -e "SELECT sms_keyword FROM kontak_group") > sms_keyword.txt)
do


i still dont understand for this part:

Code:
echo $CHECKER | grep $GRP > /dev/null
     if [ $? -eq 0 ]
     then
           CNT=$(($CNT + 1))
           case $GRP in 
           ADM)message=$message2;;
           IRC) message=$message2;;
	   PTM) message=$message2;;
            BS) message=$message2;;
           esac
#          eval SUBJECT=\$${GRP}subject
	   SUBJECT=$GRP_email_subject
#          eval ADDLIST=\$${GRP}emails
	   ADDLIST=$GRP_email_address_list
           for EMAIL_ADDRESS in $ADDLIST

where can I put the sql command?

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Smarter way to read $1 $2 in php

Hello, I am running under ubuntu14.04 with php5. When I run below php, it creates a token, then adds axaxax and bxbxbx into pc database, and at last, kills created token. What I am trying to do is to add userid and password read from a file. I do not wish to enter username and password manually.... (3 Replies)
Discussion started by: baris35
3 Replies

2. UNIX for Dummies Questions & Answers

Conditional Script

Hi, I have a script file which has some simple commands. I want these commands to be executed based on the input. Ia m good with IF statement also. At the end it has to be based on incoming value. Example CASE 1 : Execute some commands where Input value as 1 CASE 2 : Execute... (5 Replies)
Discussion started by: vrupatel
5 Replies

3. Shell Programming and Scripting

Help with conditional clauses for script output

Hello. I am new this site as well as new to shell scripting and this is my first form... Please help me with the following shell script. I am executing a shell script to run every 15 min (scheduled in cronjob) and it gives an output in an output file which is e-mailed. CONCCOUNT=`cat... (1 Reply)
Discussion started by: Jamessteevens
1 Replies

4. Shell Programming and Scripting

Conditional Execution of a Script.

I have a unix shell script Test.sh more Test.sh echo "Calling dbquery1.sh...." ./dbquery1.sh echo "Calling dbquery2.sh...." ./dbquery2.sh more dbquery1.sh sqlplus -s user1/password1@DB_SID @/tmp/storedprocedures/Hello.rcp I run Test.sh However, I do not want dbquery2.sh to be... (3 Replies)
Discussion started by: mohtashims
3 Replies

5. Shell Programming and Scripting

unix script for conditional execution

Below is my shell script. I am trying to execute two different BTEQ scripts depending on the day of the week. So on a saturday I will execute a certain BTEQ script and on other weekdays I will run the other script. #!/bin/ksh dt=`date +"%a"` if then bteq > final_output <<- EOF .run... (3 Replies)
Discussion started by: Mihirjani
3 Replies

6. Shell Programming and Scripting

Conditional Date Script

I am learning Unix for the first time. I am playing around with my default profile file to add some script in the KSH. The time format is GMT and I am in eastern time zone. I am trying to script some greetings based upon the time of day using the IF/ELIF conditions. I am getting errors. I have... (2 Replies)
Discussion started by: cpd259
2 Replies

7. Shell Programming and Scripting

Confusion over a small conditional script

Hi, I was going through a file containing hundreds of lines of codes having conditional statement written as: log() { echo $@ } num=$(/usr/bin/rsh <one_machine> -l root 'ls -d /home/user/db* 2> /dev/null' | wc -w) (( num > 0 )) && log "db info:" so,if here the return value(stored in... (2 Replies)
Discussion started by: amit4g
2 Replies
Login or Register to Ask a Question