【问题标题】:string variable interpolation [duplicate]字符串变量插值
【发布时间】:2019-06-03 04:56:50
【问题描述】:

我已经编写了这个脚本:

for i in `seq 1 $1`; do
        timestamp=$(uuidgen)
        cat $2.json | jq '.transactionId = "$timestamp"' > $3/$timestamp.json
done

我的问题是jq '.transactionId = "$timestamp"',因为文件的内容是:

{
  "transactionId": "$timestamp"
}

有什么想法吗?

【问题讨论】:

  • 您提到您遇到了特定行的问题。您能告诉我们问题是什么以及为什么会出现问题吗?
  • 把单引号改成双引号即可:jq "... '$timestamp'"
  • 作为助手,请避免使用useless catquote all your variables.

标签: bash sh jq


【解决方案1】:

有更好的方法将变量传递给jq

$ cat data.test
{
  "foo": "noooo"
}
$ new=bar
$ jq --arg new_foo "$new" '.foo |= $new_foo' data.test
{
  "foo": "bar"
}

【讨论】:

  • 在这种情况下,=|= 还不够吗?
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2012-10-26
  • 2011-03-22
相关资源
最近更新 更多