I have a bash script like this
#!/bin/bash
while read fqdn hostname; do
curl -H "Content-Type:application/json" -XPUT "https://server/api/hosts/${fqdn}" -d '{"host":{"name": "'${hostname}'"}}' --cacert bundle.pem --cert pnet-pem.cer --key privkey.pem
done <curl1.txt
The file curl1.txt contains of
fqdn (tab)hostname
..........
I have to update some data using theForeman API. I have a lot fqdns and hostnames so I've wrote the script above. The problem is with JSON, because I get an error like this:
{"status":400,"error":"There was a problem in the JSON you submitted: 795: unexpected token at '{\"host\":{\"name\": \"ptesrv02-lub\r\"}}'"}
When I put '{"host":{"name": "${hostname}"}}'
instead '{"host":{"name": "'${hostname}'"}}'
I get
{
"error": {"id":130,"errors":{"interfaces.name":["is invalid"],"name":["is invalid"]},"full_messages":["Name is invalid","Name is invalid"]}
}
So where is the problem? Can You help me with that?
To remove trivial special characters from files copied from Windows( CR-LF
endings) 'tr' command can be used as
hostname=$(echo $hostname|tr -d '\r')
in your example above. Presence of these special characters mangles how bash
treats characters.
Credits to threadp
for pointing out the presence of special characters in the hostname
variable.
If you ever suspect the file to have such CR-LF
endings, you can confirm by searching for them by using grep
, treating the file as binary
grep -U $'\015' curl1.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.