@Gleno0h said in Bash command to update a variable:
@temp what @mwittig said and following line doesnt make sense to me…
My comment was referring to the following error and the command snippets @temp sent in post 11, but I have been misled by the strange formatting he used in his post.
curl: (3) [globbing] unmatched close brace/bracket in column 7
SyntaxError: Unexpected end of input
@temp Actually, I think the problem is bad quotation. The following modified command sequence based on post 11 works for me. See also 2nd post at https://superuser.com/questions/835587/how-to-include-environment-variable-in-bash-line-curl for details.
pi@raspi3:~ $ stop_timestamp=1494910730
# Space in the date format string below must be escaped
pi@raspi3:~ $ t=`date -d@$stop_timestamp +%d-%m-%Y\ %H:%M`
# Single quotes around shell variables must be quoted!
pi@raspi3:~ $ curl -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": "'"$t"'"}' --user "a:a" http://smarthome:5001/api/variables/variable_string_today
{"variable":{"name":"variable_string_today","readonly":false,"type":"value","value":"16-05-2017 06:58","unit":""},"success":true}
EDIT: @temp In your example the inner double quotes for the variable are missing which expands to the following as the shell with quote the space character for you.... "valueOrExpression": "16-05-2017' '06:58"
. That’s causing the syntax error. You can easily trace that yourself by setting set -x
in your shell.
pi@raspi3:~ $ set -x
pi@raspi3:~ $ curl -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": "'"$t"'"}' --user "a:a" http://smarthome:5001/api/variables/variable_string_today
+ curl -X PATCH --header Content-Type:application/json --data '{"type": "value", "valueOrExpression": "16-05-2017 06:58"}' --user a:a http://smarthome:5001/api/variables/variable_string_today
{"variable":{"name":"variable_string_today","readonly":false,"type":"value","value":"16-05-2017 06:58","unit":""},"success":true}pi@raspi3:~ $
pi@raspi3:~ $
pi@raspi3:~ $ curl -X PATCH --header "Content-Type:application/json" --data '{"type": "value", "valueOrExpression": "'$t'"}' --user "a:a" http://smarthome:5001/api/variables/variable_string_today
+ curl -X PATCH --header Content-Type:application/json --data '{"type": "value", "valueOrExpression": "16-05-2017' '06:58"}' --user a:a http://smarthome:5001/api/variables/variable_string_today
curl: (3) [globbing] unmatched close brace/bracket in column 7
SyntaxError: Unexpected end of input
at Object.parse (native)
at parse (/home/pi/pimatic-dev/node_modules/pimatic/node_modules/body-parser/lib/types/json.js:88:17)
at /home/pi/pimatic-dev/node_modules/pimatic/node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/home/pi/pimatic-dev/node_modules/pimatic/node_modules/body-parser/node_modules/raw-body/index.js:262:16)
at done (/home/pi/pimatic-dev/node_modules/pimatic/node_modules/body-parser/node_modules/raw-body/index.js:251:7)
at IncomingMessage.onEnd (/home/pi/pimatic-dev/node_modules/pimatic/node_modules/body-parser/node_modules/raw-body/index.js:307:7)
at emitNone (events.js:67:13)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:923:12)
at nextTickCallbackWith2Args (node.js:458:9)
at process._tickDomainCallback (node.js:413:17)pi@raspi3:~ $
Use set +x
to switch off the trace
Hope, this helps