0

I just began with Shell script (#!/bin/bash) language. And here is a script:

begin=$(date +"%s")
item='{
      "trigram" : "REII",
      "pickupReference" : "FRTEST01",
      "alertType" : "Overloaded",
      "alertStatus" : "Opened",
      "alertDetectionDate" : "2020-08-20",
      "properties": {"Property4": "13,00 %", "Property6": "45,58 %", "Property20": "35,00 %", "Property22": "18,00" }
    },'
for ((i=1;i<=3;i++)); do  
item+='
        {
          "trigram" : "REII",
          "pickupReference" : "FRTEST01",
          "alertType" : "Overloaded",
          "alertStatus" : "Opened",
          "alertDetectionDate" : "2020-08-20",
          "properties": {"Property4": "13,00 %", "Property6": "45,58 %", "Property20": "35,00 %", "Property22": "18,00" }
        },'
done
for ((i=1;i<=2;i++)); do   
    curl --location --request POST 'https://testurl.com/data' \
    --header 'Authorization: Bearer xxxx' \
    --header 'Content-Type: application/json' \
    --header 'Cookie: BrowserId=XCVJyKw, ...' \
    --data-raw '[ '$item 
                    '{"trigram" : "REII",
                      "pickupReference" : "FRTEST01",
                      "alertType" : "Overloaded",
                      "alertStatus" : "Opened",
                      "alertDetectionDate" : "2020-08-20",
                      "properties": {"Property4": "13,00 %", "Property6": "45,58 %", "Property20": "35,00 %", "Property22": "18,00" }
                    }  ]'; 
    termin=$(date +"%s")
    difftimelps=$(($termin-$begin))
    
    if [ $i -eq 100 ] || [ $i -eq 500  ] || [ $i -eq 1000  ] || [ $i -eq 2000  ] || [ $i -eq 3000  ] || [ $i -eq 4000  ] || [ $i -eq 5000  ]; then
        echo "Alert $i - $(($difftimelps / 60)) minutes and $(($difftimelps % 60)) seconds elapsed for Script Execution.">> file_$i.txt
    fi
done

$SHELL

I want to add the variable $item to my bash script as shown above but It doesn't work, and I don't know why. Please help!

4
  • 1
    Are you trying to create an array, or just append to a single-string variable? c.f. Shellcheck.com Commented Oct 13, 2020 at 15:01
  • Append a single-string variable (the variable $item) in orther to use a list of item in the data-raw of my curl script. I've used Shellcheck.com but I didn't sort out. I'm very newbie with shell script. Thx. Commented Oct 13, 2020 at 15:08
  • 1
    Variables are only expanded inside double quotes, not inside single quotes. Commented Oct 13, 2020 at 15:10
  • 1
    I strongly recommend you use the jq utility to create JSON in shell scripts, rather than doing it by hand. It has methods to allow you to substitute variables into the JSON. Commented Oct 13, 2020 at 15:11

1 Answer 1

1

Fix the $item quoting in that curl call.

curl ... \
     --data-raw '[ '"$item"'
                 {"trigram" : "REII",
                  "pickupReference" : "FRTEST01",
                  "alertType" : "Overloaded",
                  "alertStatus" : "Opened",
                  "alertDetectionDate" : "2020-08-20",
                  "properties": {"Property4": "13,00 %", "Property6": "45,58 %", "Property20": "35,00 %", "Property22": "18,00" }
                 }  ]'; 
Sign up to request clarification or add additional context in comments.

Comments

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.