Slack message multi line from UNIX script

Hi

OS: Redhat
Version 7.5 Enterprise

Trying to post message from shell script to Slack channel and trying below code:

text="$msg"
text1="$lmsg"

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"
curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxxx/xxxxxx/xxxxxx"

However I am getting error I cant display two variables (both text and text1) in above code ). I want to post text first then text1 in next line...

However If i use only one variable.. its working fine, but it only post text variable only.. not the other..

any idea how can i concatenate in escpatedText variables both variables.

Hi,

Instead of;

escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )

Try;

escapedText=$(echo "$text $text1" | sed 's/"/\"/g' | sed "s/'/\'/g" )

Regards

Gull04

1 Like

Your code fragment: echo $text | $text1 is piping the output from echo $text into the command specified by $text1 . From your description, that doesn't seem to be what you want to do. If you had shown us how lmsg had been initialized and had shown us the diagnostics produced when you tried to run your code, this might have been more obvious.

The code gull04 suggested will concatenate the text from both variables onto a single line of output. If you want to print the contents of your two variables on separate lines, you need to put a <newline> character between the output from formatting each of your two variables. One portable way to do that would be to replace the code fragment above with:

printf '%s\n' "$text" "$text1"

If $text and/or $text1 expand to multiple lines that you're trying to flatten or contain sequences of multiple spaces that you're trying to convert to single spaces or contain tabs that you want to convert to spaces (and that is why you didn't quote them), then replace the code fragment shown above with:

{ echo $text
  echo $text1
}

or:

{ echo $text; echo $text1; }
1 Like

You can also separate with a newline, and you can use two -e options for sed, and I think you want to insert \ characters

escapedText=$(
echo "$text
$text1" | sed -e 's/"/\\"/g' -e "s/'/\\\\'/g"
)

In sed the \ character (and the & character and the separator) need to be \escaped in the substitution string.
Within a "quoted string" a \ needs to be \escaped for the shell, so "\\\\" becomes "\\" when passed to sed.
You see that a newline is retained within a "string in quotes" or a 'string in ticks'.
You further see that $( ) embeds a complete shell code that can be multi-line of course.

Usually I prefer one argument in ticks for sed:

escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)

knowing that '\'' is the escape for a literal ' within a 'string in ticks'

1 Like

Hi Folks,

A clear misunderstanding of the word "concatenate" on my part;-)

Regards

Gull04

Thank you all for reply..

My bad, i could have pasted full code.. Yes, I wanted to print those in different lines. However the above solution works Thank you @madeingermany for solution :slight_smile: However I need one more doubt the below script i am using for finding database status and posting it on slack channel.. however currently I am passing db_sid name as run time parameter which is "orcl" for example. However there are instances where I have multiple db instance in some of the environments in such case, I need the below solution to run for all db_sid instead of passing db_sid as parameter. Actually i use

 `ps -ef | grep smon | grep $db_sid | awk '{print $8}'`

. In some environments I have more than one db_sid so I need program to run against all and post status to slack.. However, tried with for loop but its was never ending.. any suggestions please.

db_sid=$1
source /home/oracle/PRD
ora_home=$ORACLE_HOME

db_st=`ps -ef | grep smon | grep $db_sid | awk '{print $8}'`
db_sid_pro="ora_smon_$db_sid"

case "$db_st" in
"$db_sid_pro")
tag="true"
msg="$db_sid database instance is running"
echo "$db_sid_pro database process is running"
;;
*)
if [ -e $ora_home/dbs/spfile$db_sid.ora ];then
msg="database is down"
echo "database is down"
tag="false"
else
tag="false"
msg="Incorrect database SID - $db_sid. Please provide the correct database SID"
echo "Incorrect database SID - $db_sid. Please provide the correct database SID"
fi
;;
esac

lsn=`lsnrctl status | grep successfully | awk '{print $4}'`
case "$lsn" in
"successfully")
lmsg="Oracle listener is up and running"
echo "Oracle listener is up and running"
ltag="true"
;;
*)
lmsg="Oracle listener is down please make it up"
echo "Oracle listener is down please make it up"
ltag="false"
;;
esac

if [ $tag = "false" ]; then
echo "$msg"
fi

if [ $ltag = "false" ]; then
echo "$lmsg"
fi

slackhost="slackb"

token="xxxxxxxxxxxxxxxxxxxxx"

if [[ $token == "" ]]
then
        echo "No token specified"
        exit 1
fi

shift
channel="#are_you_listening"
if [[ $channel == "" ]]
then
        echo "No channel specified"
        exit 1
fi

shift

text="$msg"
text1="$lmsg"

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

#escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/; s/'\''/\\'\''/g'
)
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxx/xxx/xxxxxx

Please add the missing / g (I have correct my post#4):

escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)

The

ps -ef | grep smon | grep $db_sid

is error-prone (unsharp grep might catch wrong lines, grep command itself might appear in the ps output)
Could you give an example result of it?

Apologies for delayed reply

The below is smon process shows two db running one is orcl and another one orcltest. So I want program to run for both these and should show its status.

ps -ef | grep smon 
[oracle@ip-xx-xx-xx-xxx~]$ ps -ef|grep smon
oracle   15093     1  0 Nov08 ?        00:00:30 ora_smon_orcl
oracle   15486     1  0 Nov08 ?        00:00:30 ora_smon_orcltest

Maybe you can do

db_sid_pro="ora_smon_$db_sid"
if pgrep -u oracle -x "$db_sid_pro" >/dev/null 
then
  tag="true"
  msg="$db_sid database instance is running"
  echo "$db_sid_pro database process is running"
else
  ...
fi

The shell script uses the exit status from pgrep and throws its result away. (Alternatively you could capture the result in a variable and then test it for having a value: pid=`pgrep ...`; if test -n "$pid" )
The -u oracle and -x options make the match even more precise.
See man pgrep .

Hi,

I tried to loop in the logic for all db_sid that are currently running and modified logic as below..but program running but wrong results even though database and listners are up when i checked manually
existing oracle instance running

[oracle@ip-xx-xx-xx-xxx~]$ ps -ef|grep smon
oracle   15093     1  0 Nov08 ?        00:00:30 ora_smon_orcl
oracle   15486     1  0 Nov08 ?        00:00:30 ora_smon_orcltest

Now trying to get each instance that is running ..i.e only db_sid name by omitting ora_smon_ and getting only name

for ora_smon_orcl ==> orcl
for ora_smon_orcltest ==> orcltest


for db_sid in `ps -ef | grep smon | grep -v grep | awk '{ print $8 }' | cut -d '_' -f3` ; do
echo $db_sid

db_sid_pro="ora_smon_$db_sid"
echo $db_sid_pro
source /home/oracle/$db_sid
ora_home=$ORACLE_HOME
echo $ORACLE_HOME

case "$db_sid" in
"$db_sid_pro")
echo "hi"
tag="true"
echo $tag
msg="$db_sid database instance is running"
echo "$db_sid_pro database process is running"
;;
*)
if [ -e $ora_home/dbs/spfile$db_sid.ora ];then
msg="database is down"
echo "database is down"
tag="false"
else
tag="false"
msg="Incorrect database SID - $db_sid. Please provide the correct database SID"
echo "Incorrect database SID - $db_sid. Please provide the correct database SID"
fi
;;
esac

the output shown on terminal is below: but manually i checked databse is up and listener also up..May be i am missing something in programming logic here not really database issue

orcl
ora_smon_orcl
/dbusr/app/oracle/product/12102
database is down
Oracle listener is down please make it up
database is down
Oracle listener is down please make it up
orcltest
ora_smon_orcltest
/dbusr/app/oracle/product/12102
database is down
Oracle listener is down please make it up
database is down
Oracle listener is down please make it up

However I find below is causing trouble..because when I put some debug echo its not displaying "hi"

case "$db_sid" in
"$db_sid_pro")
echo "hi"
tag="true"
echo $tag

and also i have manually checked for below line.. files are indeed exist as per below conditon

if [ -e $ora_home/dbs/spfile$db_sid.ora ];then

Any suggestion

If the loop goes over the running processes, how are the not running processes detected then?
The following loops over the files to get the DB instances, then looks them up in the ps output:

# turn debugging on
#set -x

ora_home=$ORACLE_HOME

#pscope="-e"
pscope="-u oracle"
ps_prefix=ora_smon_
# ora_ps is a newline-separated list of matching ps args
ora_ps=$(
  ps $pscope -o args= | awk -v prefix="$ps_prefix" '$1~prefix {print $1}'
)
# prepend and append a newline separator for an exact lookup:
ora_ps_lu="
$ora_ps
"
#echo "The oracle processes are:$ora_ps_lu"

# For each DB there is a file - but perhaps no process
# So it makes sense to loop over the files

spfile_prefix=$ora_home/dbs/spfile
spfile_suffix=.ora
for file in "$spfile_prefix"?*"$spfile_suffix"
do
  # ensure it's a file
  [ -f "$file" ] || continue
  # dig out the db_sid
  db_sid=${file#$spfile_prefix}
  db_sid=${db_sid%$spfile_suffix}
  db_sid_pro=${ps_prefix}${db_sid}
  # for an exact lookup prepend and append the separator (newline)
  db_sid_pro_lu="
$db_sid_pro
"
  # match in the $ora_ps_lu
  case "$ora_ps_lu" in
  *"$db_sid_pro_lu"* )
    echo "$db_sid database instance is running"
    echo "$db_sid_pro process is running"
  ;;
  * )
    echo "$db_sid database instance is down"
    echo "No $db_sid_pro process"
  ;;
  esac
done

It avoids bash4 arrays - instead it does a lookup in a simple variable.

1 Like

Thank you very much

Its worked like a charm. bit modified to display output like below and terminal output is like:

==============================================
Oracle Host: development_box
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

However, as you suggested I am using below line to slack...however only first two line going into slack..

the below is code currently being used for slack post:

token="xxxxxxxxxxxxxxxxxxxxx"

if [[ $token == "" ]]
then
        echo "No token specified"
        exit 1
fi

shift
channel="#are_you_listening"
if [[ $channel == "" ]]
then
        echo "No channel specified"
        exit 1
fi

shift

text="$msg"
text1="$lmsg"

if [[ $text == "" ]]
then
        echo "No text specified"
        exit 1
fi

#escapedText=$(echo $text | $text1 | sed 's/"/\"/g' | sed "s/'/\'/g" )
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxx/xxx/xxxxxx

On terminal I am getting all output but, on slack i get only the below values

orcltest Database UP -- OK
Listener UP -- OK

no clue why it printing only these lines.. do I need to print instead of echo ?. any suggestion pls
Is there any way to send terminal screen output same format to slack as it is?

------ Post updated at 01:13 PM ------

Hi,

Some how i am able to output all values in terminal.. Up to this point i am able to achieve...now problem is with posting it to slack..even though I mention \n its not printing line by line in slack...

Terminal output (I have stored into variable, so echo it):

echo "$text"

==============================================
Oracle Host: xxxxxxxxx
==============================================
orcl:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------
orcltest:
Database UP -- OK
Listener UP -- OK
Database Read&Write Mode -- OK
Instance Status -- OK
Instance Login Allowed -- OK
---------------------------------------------

Now trying to read line by line from this variable with below code:

while IFS= read -r line; do
  #printf '%s\n' "$line"
  text="$text$line\n"
done <<< "$text"

escapedText=$(echo $text | sed 's/"/\\"/g; s/'\''/\\'\''/g' )
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}"

curl -X POST --data-urlencode "payload=$json" "https://hooks.slack.com/services/xxxxx/xxxx/xxxxx"

its showing error like:

missing_text_or_fallback_or_attachments

but in above code I guess while loop is not working properly...is that correct way of reading multi line variable and pass on to payload in curl statement

This is resolved now. Omitting loop for text variable and updating escapedtext = "$text" does the trick..

Thank you for all help and patience..

Shell variables in command arguments must always in "quotes", for example: echo "$text" (otherwise the shell tries expansions on it.)
Maybe you can add another sed command that changes newlines to \n (two characters):

printf "%s\n" "$text" "$text1" | sed '
s/"/\\"/g; s/'\''/\\'\''/g
H;1h;$!d;x;s/\n/\\n/g
'