0

Below is my bash script.

#!/bin/bash
message=$1
hostname=$2
severity=$3
eventname=$4
tagpath=$5
appname=$6
data="{"action":"EventsRouter","method":"add_event","data":[{"summary":"$message"},"device":"$hostname","message": "$message $eventname $tagpath","component":"$appname","severity":"$severity","evclasskey":"nxlog","evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":1}"

echo "Total number of args : $#"

echo "message = $message"
echo "hostname = $hostname"
echo "appname = $appname"
echo "data = $data"

curl -u uname:password -k https://myurlcom/zport/dmd/evconsole_router -d $data

and when i try to run with sh tcp.sh value value value value value value

host:
'host,component:host,severity:host,evclasskey:nxlog,evclass:/nxlog/perf,monitor:localhost}],type:rpc,tid:1}'
is not a legal name (unexpected end of input) Total number of args : 6
message = message hostname = test appname = host data = curl: option
-d: requires parameter

I see that data has no value included. This json has to be sent in this order for it to be accepted in the endpoint. Help me correct this.

1
  • I recommend using a tool like jq to generate valid JSON, rather than relying on parameter expansion to do so. Commented May 14, 2020 at 16:09

2 Answers 2

2

Using jq to safely generate the desired JSON:

#!/bin/bash

parameters=(
  --arg message "$1"
  --arg hostname "$2"
  --arg severity "$3"
  --arg eventname "$4"
  --arg tagpath "$5"
  --arg appname "$6"
)

data=$(jq -n "${parameters[@]}" '
  {action: "EventsRouter",
   method: "add_event",
   data: [ {summary: $message, 
            device: $hostname,
            message: "\($message) \($eventname\) \($tagpath)",   
            component: $appname,
            severity: $severity,
            evclasskey: "nxlog",
            evclass: "/nxlog/perf",
            monitor: "localhost"
            }
          ],
   type: "rpc",
   tid: 1
  }'


curl -u uname:password -k https://myurlcom/zport/dmd/evconsole_router -d "$data"
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming:

message="my_message"
hostname="my_host"
severity="my_severity"
eventname="my_event"
tagpath="my_path"
appname="my_app"

If you run:

data="{"action":"EventsRouter","method":"add_event","data":[{"summary":"$message"},"device":"$hostname","message": "$message $eventname $tagpath","component":"$appname","severity":"$severity","evclasskey":"nxlog","evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":1}"

you will get an error, because there is a not escaped white space before the string "my_event"

my_event: command not found

What happened? Since your json input has a lot of words between double quotes, you will have to enclose the whole string into single quotes, in order to preserve the double quotes inside of the string. But between single quotes, the bash variables will not be replaced by their value. So you will need to close the single quotes before each variable and reopen these again immediately after.

So that line of your script must become:

data='{"action":"EventsRouter","method":"add_event","data":[{"summary":"'$message'"},"device":"'$hostname'","message": "'$message $eventname $tagpath'","component":"'$appname'","severity":"'$severity'","evclasskey":"nxlog","evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":1}'

If you execute:

echo "$data"

you will get:

{"action":"EventsRouter","method":"add_event","data":[{"summary":"my_message"},"device":"my_host","message": "my_message my_event my_path","component":"my_app","severity":"my_severity","evclasskey":"nxlog","evclass":"/nxlog/perf","monitor":"localhost"}],"type":"rpc","tid":1}

which is correct, I assume: the double quotes didn't disappear from your json data structure.

6 Comments

This is not working for me, i also realize there is an extra } after "summary":"my_message"} , which i corrected, and provided "'$message'". @Pierre
Probably, there are more errors, but my answer fixes at least one.
I removed the concatenated part and then it seemed to work. Is this the only way of concatenating multiple fields in bash?
I think "message": "my_message" "my_event" "my_path" is wrong. The value should be only one string instead of three. I corrected that also into "message": "my_message my_event my_path".
Okay, If i do want to have them concatenated, how do i do it? just curious @Pierre
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.