Hi,
I wanted to create a bash script that takes a list of JSON files inside a repository and try to send each documents contained inside of them to the instance of my Sync Gateway. If the _id
of the document was already existing, the script should overwrite it (adding the _rev
attribute). I manage to do my script but I’m stuck at one detail: special characters contained inside my documents aren’t well interpreted (they became “?”, I suppose it means my data aren’t sent/received as UTF-8 encoded data. But I do not understand why. Here is my script (my very first bash script, so they might be things to improve) that I’m running with Cygwin64 Terminal:
#!/bin/bash
# $1 = directory containing JSON files
# $2 = server url (+ port)
# $3 = database name
#Loop on every file of the given directory
for file in "$1"/*; do
#Try to insert every document of a specific JSON file and retrieve their status (200 = OK; 409 = conflict)
allStatus=$(curl -H "Content-Type: application/json" -H "Cache-Control: no-cache" --data-binary "@$file" $2/$3/_bulk_docs --silent | jq '.[] |.status' | tr -d '\r')
docIndex=0
#Loop on every status (one status = one document)
while IFS=' ' read -ra statusArray; do
for status in "${statusArray[@]}"; do
#Remove unwanted windows characters of the file
sed -i 's/\r//g' $file
fileContent=`cat $file`
#Retrieve the id of the current document
id=`jq -r -j ".docs[$docIndex]._id"<<<"$fileContent" | tr -d '\r'`
if [ "$status" = "409" ]
then
#Retrieve the revision of the current document and add it to the document
rev=$(curl -X GET --header 'Accept: application/json' $2/$3/$id?revs=true --silent | jq -r -j '._rev' | tr -d '\r')
result=$(jq -c -j ".docs[$docIndex] + { \"_rev\": \"$rev\" }"<<<"$fileContent")
#Wrap the document inside {"docs":[]} -> unique format accepted by Couchbase
result="{\"docs\":[$result]}"
#Remove unwanted windows characters before sending the document (again)
result="$( echo "$result" | sed 's/\r//g')"
result="$( echo "$result" | iconv -t 'UTF-8')"
s=$(curl -H "Content-Type: application/json; charset=UTF-8" -H "Cache-Control: no-cache" -d "$result" $2/$3/_bulk_docs --silent)
#Echo result
echo $s
else
#Status should be 200.
echo 'status: '$status ' - ' $id
fi
docIndex=$((docIndex+1))
done
done <<< "$allStatus"
done
Any idea what’s wrong here? Many thank’s !
– I’m under Couchbase Server 4.5.1 community / Sync Gateway 1.4.0
Their is also a question on StackExchange here: https://unix.stackexchange.com/q/372224/236252