0

I have a script execute.sh which receive a JSON with a curl call

#!/bin/bash

source getoutput.sh
server="$1"
cmd="$2"

res=$(curl -sS http://$server -X PUT -H "Content-Type: application/json" -d "{ \"command\" : \"$cmd\" }")

when running this ./execute.sh 246.X.X.X:2000 "stop FXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX"

This is working without any errors

but when running this in a while loop

while read id; do
./execute.sh 246.X.X.X:2000 "stop $id"
done < id

it throws parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 6, column 74

Output différences :

Working:

+ source getoutput.sh
+ server=246.X.X.X:2000
+ cmd='stop FXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX'
++ curl -sS http://246.X.X.X:2000 -X PUT -H 'Content-Type: application/json' -d '{ "command" : "stop FXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX" }'
+ res='{
  "status" : 200,
  "status_message" : "OK",
}'

Not working when using while loop:

+ source getoutput.sh
+ server=246.X.X.X:2000
+ ' cmd='stop FXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX
++ " }'url -sS http://246.X.X.X:2000 -X PUT -H 'Content-Type: application/json' -d '{ "command" : "stop FXXXXXXXXXXX-XXXXXXXX-XXXXXXXX-XXXX
+ res='{
      "status" : 400,
      "status_message" : "Bad Request",
}'

jq version : 1.5 , bash version : 4.3.48

  • 2
    it's not because you run it in a loop, but because you'reading from a dos/windows file with lines terminated by cr/lf. Trim your id file with dos2unix or similar. – Uncle Billy Jun 17 at 20:35
  • Also consider using jq --arg varname "$value" to import a shell variable correctly (including correct encoding) into jq. See tho jq manual. – Kusalananda Jun 17 at 21:37
  • dos2unix command worked, thanks! – Nani Jun 18 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.