Slack message multi line from UNIX script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Slack message multi line from UNIX script
# 1  
Old 11-06-2018
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:

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.
# 2  
Old 11-06-2018
Hi,

Instead of;

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

Try;

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

Regards

Gull04
This User Gave Thanks to gull04 For This Post:
# 3  
Old 11-06-2018
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:
Code:
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:
Code:
{ echo $text
  echo $text1
}

or:
Code:
{ echo $text; echo $text1; }

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-06-2018
You can also separate with a newline, and you can use two -e options for sed, and I think you want to insert \ characters
Code:
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:
Code:
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)

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

Last edited by MadeInGermany; 11-06-2018 at 08:13 AM.. Reason: /g modifier missing in the last sample
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 11-06-2018
Hi Folks,

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

Regards

Gull04
# 6  
Old 11-06-2018
Quote:
Originally Posted by MadeInGermany
You can also separate with a newline, and you can use two -e options for sed, and I think you want to insert \ characters
Code:
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:
Code:
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/; s/'\''/\\'\''/g'
)

knowing that '\'' is the escape for a literal ' within a 'string in ticks'
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 Smilie 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
Code:
 `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.

Code:
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

# 7  
Old 11-06-2018
Please add the missing /g (I have correct my post#4):
Code:
escapedText=$(
echo "$text
$text1" | sed 's/"/\\"/g; s/'\''/\\'\''/g'
)

The
Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge multi-lines into one single line using shell script or Linux command

Hi, Can anyone help me for merge the following multi-line log which beginning with a " and line ending with ": into one line. *****Original Log***** 087;2008-12-06;084403;"mc;;SYHLR6AP1D\LNZW;AD-703;1;12475;SYHLR6AP1B;1.1.1.1;0000000062;HGPDI:MSISDN=12345678,APNID=1,EQOSID=365;... (3 Replies)
Discussion started by: rajeshlinux2010
3 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. UNIX for Beginners Questions & Answers

Script to accept Multi line inputs

Hi there, I'm trying to create a script that will accept multiple inputs by copying and pasting the strings from a notepad, hit Enter key and output the string to a text file.I'm thinking of using the read command however it will just simply get the first line. Apologies but got no idea how... (7 Replies)
Discussion started by: norbie.lopez
7 Replies

4. Shell Programming and Scripting

Reading Multi Line SQL in UNIX

Hello, Currently, I am reading few queries from the below .sql file --SOURCE TABLE NAME --SOURCE QUERY SEL COL1, COL2, COL3, COL4, COL5, COL6, COL7 WHERE COL5 = '2015-11-04 16:24:00.000000' FROM SOURCE TABLE; --TARGET TABLE NAME --TARGET QUERY SEL COLUMN1, COLUMN2, COLUMN3, COLUMN4,... (4 Replies)
Discussion started by: ronitreddy
4 Replies

5. Shell Programming and Scripting

Sed: deleting last line prevents '$' address from working in the multi-script invocation

It looks like if matching and deleting the last line confuses 'sed' so it does not recognize '$' address. Consider: sed -e '/^3/d' -e '$ a text' supposed to delete a line starting with '3' and then append 'text' after the last line of input. But, if it is the last line of input which starts... (2 Replies)
Discussion started by: msz59
2 Replies

6. Shell Programming and Scripting

Removing SAS multi line comments in UNIX

i have to remove the commented (/* . . . .*/) part which starts in one line and ends in other.help me with generic code because i have 1000 to 10k lines code which i have to remove. data one; set work.temp; input name age; infile filename; /* dfsdf dsfs sdfdf dsdd sdfsf sdfsf sfs... (4 Replies)
Discussion started by: saaisiva
4 Replies

7. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

8. Shell Programming and Scripting

SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help. What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into... (3 Replies)
Discussion started by: Alivadoro
3 Replies

9. Shell Programming and Scripting

Multi line variable script... needs help.

I am trying to write a script that will help me put a file into excel with little manipulation. Below is a sample of the file im using. Group1:*:gid1:user,user Group2:*:gid2:user,user Group3:*:gid3:user,user,user,user,user,user,user Group4:*:gid4:user,user I marked in red the part that is... (1 Reply)
Discussion started by: rookieuxixsa
1 Replies

10. Programming

message queues and multi-process

Hi, Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer... (10 Replies)
Discussion started by: rvan
10 Replies
Login or Register to Ask a Question