R
R
Ruslan2014-05-16 09:47:38
linux
Ruslan, 2014-05-16 09:47:38

Bash script - how to split data?

Hello!
Help with script.

#! /bin/bash
to=$1
subject=$2
body=$3

ack=`echo "select message FROM table WHERE id='$body'" | mysql -uuser -ppassword -Dbase`
cat <<EOF | mail -s "$subject" "$to"
$ack
EOF

In this form, everything works, mail is sent, but at the same time, the body=$3 variable can contain several data separated by anything (I can set it myself with handles, for example data1; data2; data3).
How can this data be shared? So that ack leaves, for example, the first value of body - data1. Could subsequent values ​​be inserted into the mail output?
Probably, to make it clearer, this script is for zabbix, for sending notifications.
Zabbix is ​​configured to send mail via the external script presented above. From the zabbix documentation:
"When the notification script is executed, it is passed to it via command line 3 ($1, $2 and $3 respectively): To Subject Message"

So, data like this is transferred to $3: {EVENT.ID};{INVENTORY.LOCATION1};{TRIGGER.STATUS};{TRIGGER.SEVERITY};{EVENT.DATE};{EVENT.TIME}
The first from the list is {EVENT .ID} is exactly what is needed for a mysql query. The rest is required to be transferred in the body of the letter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentine, 2014-05-16
@Pumko_adm

#! /bin/bash
to=$1
subject=$2
body=$3

ack=`echo "select message FROM table WHERE id='$(echo $body | cut -d\; -f1)'" | mysql -uuser -ppassword -Dbase`
cat <<EOF | mail -s "$subject" "$to"
$(echo $body | cut -d\; -f2-)
EOF

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question