【问题标题】:How to avoid bash/shell skipping quotation in variable value?如何避免 bash/shell 在变量值中跳过引号?
【发布时间】:2022-06-22 17:19:06
【问题描述】:

我在下面设置了一个这样的变量-

domain= ("*.abc" "*.xyz" "*.123")

我想在下面的 json 文件中设置这个变量的值-

"Items": [
            "*.abc",
            "*.xyz",
            "*.123",]

但是,问题是 bash 脚本跳过引号 "" 并且只在引号内。除此之外,bash 还试图将值作为命令。我只想在 Items 数组中设置值,包括逗号,就是这样。

我正在使用jq --arg e1 ${domain[@]}将域变量设置为e1环境变量。

并得到以下错误 -

jq: error: syntax error, unexpected '*', expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1: *.xyz.com

【问题讨论】:

  • --arg 不理解 bash 数组(某些 shell 没有任何数组)。
  • 如果你想将引号作为字符串的一部分,你必须在字符串中实际写入一个,例如domain= ('"*.abc"' '"*.xyz"' '"*.123"')

标签: json bash shell variables jq


【解决方案1】:

--arg 不理解 bash 数组(某些 shell 没有任何数组)。

您可以改用--args,它使用剩余参数列表填充$ARGS.positional

domain=("*.abc" "*.xyz" "*.123")
jq '.Items = $ARGS.positional' <<<'{"Items":[]}' --args "${domain[@]}"

请注意,我删除了domain= 之后的空格。使用空格,bash 会抛出语法错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2021-01-25
    • 2012-06-24
    • 1970-01-01
    • 2015-10-08
    相关资源
    最近更新 更多