0

In the below dev-netpro.sh script 'curl' does not post anything if my $FWK (i.e. '8fwk3_dev 2fwk0_dev' - could be up to 9 words) and $CON (in the example it is series of digits:'72 43 172 28021 8261 14015 325 24 524') get more than one word

[Expert@WAL-NEW-VSX-02:0]# cat dev-netpro.sh 
#!/bin/bash
while true; do
        FWK=$(top -b -n 1 | egrep 'fwk' |  awk '{print $9 $NF}')
        CON=$(vsx stat -l | egrep 'number' | awk '{print $3}')       
TIME=`date +"%T"` 
echo $HOSTNAME, $TIME, $FWK, $CON
echo '{"WAL-VSX-02": "['"$TIME"','"$FWK"','"$CON"']"}'
curl -ik -H "Accept: application/json" -H "Content-type: application/json" -d '{"WAL-VSX-02": "['"$TIME"','"$FWK"','"$CON"']"}' -X POST "https://10.199.107.11:8880/test"  
sleep 2 
done
[Expert@WAL-NEW-VSX-02:0]# 

See the output below:

    [Expert@WAL-NEW-VSX-02:0]# ./dev-netpro.sh 
    WAL-NEW-VSX-02, 12:53:17, 8fwk3_dev 2fwk0_dev, 72 43 172 28021 8261 14015 325 24 524
    {"WAL-VSX-02": "[12:53:17,8fwk3_dev
    2fwk0_dev,72
    43
    172
    28021
    8261
    14015
    325
    24
524]"}

......nothing

3 Answers 3

1

Newline came from these two commands run at the beginning of the script . After

FWK=$(echo $FWK|tr -d '\n') CON=$(echo $CON|tr -d '\n')

it worked ! Appreciate your help a lot

0

You need quotes around that content inside of the JSON object try and you need to quote the entire string so that spaces inside the variables don't make anything after the space get treated as a separate argument.

echo "{\"WAL-VSX-02\": [\"$TIME\",\"$FWK\",\"$CON\"]}"

The way you had it before there were (single) quotes around several components of the string which works but if there are spaces in any of the vars then the shell parses it as separate strings. This way the whole thing is quoted so the shell will parse it as one string.

7
  • no change after -d '{"WAL-VSX-02": "["'"$TIME"'","'"$FWK"'","'"$CON"'"]"}' . It worked well if $FWK was just 8fwk3_dev and $CON was i.e. 34 Commented Dec 3, 2015 at 18:20
  • i.e. {"WAL-VSX-02":"[13:21:52,4fwk3_dev,27512]"} it works well Commented Dec 3, 2015 at 18:23
  • See my edits. There was a second problem that I hadn't noticed at first. Commented Dec 3, 2015 at 18:30
  • same thing, I think problem is that there are \n between words.. Commented Dec 3, 2015 at 20:44
  • How did the \n's get there? When you did the first echo it was just spaces between the words. Commented Dec 3, 2015 at 20:45
0

To include arbitrary strings in a JSON document, you need to encode the strings as JSON strings properly. This is best done with a utility that understands JSON, such as jq:

#!/bin/bash

while true; do
    fwk=$( top -b -n 1 | awk '/fwk/ { print $9 $NF }' )
    con=$( vsx stat -l | awk '/number/ { print $3 }' )

    printf -v now '%(%T)T' -1

    jq -cn \
        '{ "WAL-VSX-02": $ARGS.positional }' \
        --args "$now" "$fwk" "$con" |
    curl --silent --show-error \
        --json @- 'https://10.199.107.11:8880/test'

    sleep 2
done

In the above code, a JSON document is created using jq. The document will contain a single JSON object with the key WAL-VSX-02. The value of the key will be an array whose elements are the strings listed after --args at the very end of the command.

The JSON object is piped to curl which reads it with --json @-.

To see the posted JSON in each iteration, insert tee as a new pipeline step between jq and curl.

Given the data that I can see in your question, this would generate and post a JSON document equivalent to

{
  "WAL-VSX-02": [
    "12:53:17",
    "8fwk3_dev\n2fwk0_dev",
    "72\n43\n172\n28021\n8261\n14015\n325\n24\n524"
  ]
}

(But on a single line, due to the -c option we use with jq.)

With a tiny bit more effort, we could post those multi-line strings as separate arrays. Just use a slightly modified jq expression that splits the given strings (apart from the first) on newlines:

jq -cn \
    '{ "WAL-VSX-02": ( $ARGS.positional | .[1:] |= map(split("\n")) ) }' \
    --args "$now" "$fwk" "$con"

This would generate the equivalent of

{
   "WAL-VSX-02": [
      "12:53:17",
      [ "8fwk3_dev", "2fwk0_dev" ],
      [ "72", "43", "172", "28021", "8261", "14015", "325", "24", "524" ]
   ]
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.