I'm using bash select menu's to create a quick cli to interact with an API. Right now all is working well with the individual/prompted show, add, update, delete. I'm having a problem with the bulk "operation" which will be reading from a file, e.g:
10.10.0.0/20,test,true
100.100.1.2/32,test,false
11.11.1.0/28,test,false
and set the values as variables into a payload variable. This is the variable working well in all code.
PAYLOAD="{\"prefix\": \"$prefix\", \"comment\": \"$comment\", \"enabled\": $status}"
To read from the file and split the commas I'm doing:
#!/bin/bash
# To read values from file
#set -x
file="`pwd`/ips.txt"
while IFS=, read -r f1 f2 f3
do
PAYLOAD="{\"prefix\": \"$f1\", \"comment\": \"$f2\", \"enabled\": $f3}"
echo "$PAYLOAD"
#curl_api "$url" "POST" "$PAYLOAD"
done < $file
However when displaying the payload i'm getting the payload variable with the curly brackets all messed up:
}"prefix": "100.10.0.0/20", "comment": "test", "enabled": true
}"prefix": "100.100.1.2/32", "comment": "test", "enabled": false
}"prefix": "11.11.1.0/28", "comment": "test", "enabled": false
This makes the API call to fail... and I'm struggling to understand why the brackets don't open/close {} as expected:
{"prefix": "100.10.0.0/20", "comment": "test", "enabled": true}
{"prefix": "100.100.1.2/32", "comment": "test", "enabled": false}
{"prefix": "11.11.1.0/28", "comment": "test", "enabled": false}
Thanks in advance to anyone that takes time to read and help on this post.