LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-12-2019, 06:44 AM   #1
siparker
LQ Newbie
 
Registered: Jun 2019
Location: UK
Distribution: Ubuntu 18
Posts: 1

Rep: Reputation: Disabled
How to include a variable into a json formatted item in a bash script.


I am using a bash script to run a for loop to Rclone my backups to Backblaze.

I am able to muddle my way through making a for loop so it processes each backup folder one at a time.

I have been trying to expand on this slightly to send a slack notification when each one is finished.

the notification works ok but I can't find out how to include the current name in the JSON that gets sent.

My code is as follows

Code:
#!/bin/bash
# A Simple Rclone Loop
names='user1 user2 user3 user4'

for name in $names
do
rclone sync /home/$name/backup backblaze:my-bucket/$name -v --bwlimit 9M --log-file=/path/to/rcloneupload.log

curl -X POST -H 'Content-type: application/json' --data '{"text":"Backup Finished"}' https://hooks.slack.com/services/xxxxxxx-myslackwebhook-xxxxxxxxx
done
Its how to get the current $name into the JSON that gets sent to slack.

What i want is essentially

Code:
'{"text":"$name Backup Finished"}'
that doesn't get parsed.

I have tried to concatenate things and it says its invalid JSON.

Sorry for the noob trying to do complex things post. Any help would be appreciated.

Extra imaginary internet points if I can get the size of the files transferred from Rclone into the slack message
 
Old 06-12-2019, 07:21 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware 14.2 current / ArcoLinux / Void Linux
Posts: 8,669

Rep: Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802Reputation: 1802
ballparking here..

..maybe..
Code:
'{"text":'"$(echo -e $name)"' Backup Finished"}'
OR
'{"text":"'$name' Backup Finished"}'
something like that. maybe mess the quotes to see which ones need to be where. If you got spaces within the string in the variable too maybe double quotes and single quotes.

Last edited by BW-userx; 06-12-2019 at 07:28 AM.
 
Old 06-12-2019, 10:26 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,890

Rep: Reputation: 1169Reputation: 1169Reputation: 1169Reputation: 1169Reputation: 1169Reputation: 1169Reputation: 1169Reputation: 1169Reputation: 1169
I got around it this way, this produces a "rich" Slack message. I'm sure you can work out the best way to use it for your own needs.

Code:
DATA=$(cat << END_MESSAGE
{
    "attachments": [
        {
            "fallback": "${TEXT}",
            "color": "${COLOR}",
            "title": "${TYPETEXT} Notification",
            "text": "${TEXT}"
        }
    ]
}
END_MESSAGE
)

curl -X POST -H "Content-type: application/json" --data "${DATA}" ${URL}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending Json formatted logs through rsyslog kkrrss Linux - Server 1 08-25-2018 06:59 AM
How to echo a variable as an item in a Bash array? justmy2cents Programming 8 08-24-2017 05:13 PM
[SOLVED] Get bc script variable into a bash script variable? 14hei Programming 6 02-26-2016 07:34 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
mozilla xpm kmenu item desktop item cjae Linux - Newbie 3 04-06-2005 07:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration