Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-12-2019, 06:44 AM   #1
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

# A Simple Rclone Loop
names='user1 user2 user3 user4'

for name in $names
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"}'
Its how to get the current $name into the JSON that gets sent to slack.

What i want is essentially

'{"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
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..

'{"text":'"$(echo -e $name)"' Backup Finished"}'
'{"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
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.

    "attachments": [
            "fallback": "${TEXT}",
            "color": "${COLOR}",
            "title": "${TYPETEXT} Notification",
            "text": "${TEXT}"

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


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 > Forums > Linux Forums > Linux - Newbie

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

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