0

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)

  • You should probably be passing your values to 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:40
  • It would be more helpful to us if you could explain what it is that you try to do. You seem to go about doing whatever it is, in a very complicated way. Seeing the original JSON document and what you want to change in this would be good. – Kusalananda May 07 '21 at 15:41
  • Should we assume this is a bash script? Or is it sh? Something else? – terdon May 07 '21 at 15:42
  • FYI, there's no such thing as a "command variable". Variables exist, and shells are capable of command substitution, but a variable is a variable no matter where it gets its value from, its properties remain the same, and it can be used in the same ways regardless of where its value came from. A better way of phrasing what you wrote is "the variable gets its value from date via command substitution". Or something like to that. – cas May 07 '21 at 15:55
  • Now there is the full source: https://github.com/DaniellMesquita/Web3Updater – Daniella Mesquita May 07 '21 at 16:03
  • You have the variable $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
  • @ilkkachu I tried a lot of tricks and didn't worked. Edited the description to focus in the part that isn't working – Daniella Mesquita May 08 '21 at 01:50
  • @DaniellMesquita, ok, so, this '.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

0 Answers0