Source part:
if [ $(jq -r '.patch_at' update.json) -ge "4" ]; then
recentlycheckedat=$(echo '('`date +"%s.%N"` ' * 1000000)/1' | bc)
contents="$(jq '.recently_checked_at = "$recentlycheckedat"' update.json)" && \
echo "${contents}" > update.json # have to fix, its literally writing $recentlycheckedat
fi
Focus:
contents="$(jq '.recently_checked_at = "$recentlycheckedat"' update.json)"
How to insert the content of the variable "recentlycheckedat" instead of literally printing out "$recentlycheckedat"?
It stores the current timestamp as a command on a variable, but this variable can't work inserted in a command inside another variable. How to achieve this? And feel free to edit with an more appropriate title.
full source - https://github.com/DaniellMesquita/Web3Updater
Update - solution:
".recently_checked_at = "$recentlycheckedat"" (many thanks to @ilkkachu)
jq
using--arg
or--argjson
, rather than trying to embed shell variables into the filter directly. See Invoking jq – steeldriver May 07 '21 at 15:40bash
script? Or is itsh
? Something else? – terdon May 07 '21 at 15:42date
via command substitution". Or something like to that. – cas May 07 '21 at 15:55$recentlycheckedat
within single quotes, where it doesn't get expanded, see: https://unix.stackexchange.com/questions/503013/what-is-the-difference-between-the-and-quotes-in-th – ilkkachu May 07 '21 at 17:46'.recently_checked_at = "$recentlycheckedat"'
is a single-quoted string. Within one, the double-quotes aren't special, and neither is the dollar sign, so the variable isn't expanded, but you get the literal$recentlycheckedat
instead. You need to put the whole thing in double quotes, and then escape the inner double quotes:".recently_checked_at = \"$recentlycheckedat\""
(or change the inner ones to single quotes, if that's possible in this context) – ilkkachu May 08 '21 at 10:31