不匹配的关闭 brace/bracket

Unmatched close brace/bracket

我试图在 bash 中执行一个脚本,但抛出这个错误,

curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 24:
UTC","user_id":"01234"}}

我试过去掉牙套但是没用,就是这条线,

response=$(curl  -X POST -H "Authorization: Bearer ${bearer_token}" -H "Content-Type: application/json" -d '{"cursus_user":{"begin_at":"'${start}'","cursus_id":"'${cursus_id}'","end_at":"'${end}'","user_id":"'${user}'"}}' "https://xxxxxx/xxxxxx.com")

有人知道问题出在哪里吗?我被卡住了,在此先感谢。

[更新]

我真的看不出错误:(

'
{
    "cursus_user":
    {
        "begin_at":     "'${start}'",
        "cursus_id":    "'${cursus_id}'",
        "end_at":       "'${end}'",
        "user_id":      "'${user}'"
    }
}
'

Shellcheck 标识了 curl 命令中的几个未加引号的变量。它甚至提供更正后的代码:

response=$(curl -X POST -H "Authorization: Bearer ${bearer_token}" -H "Content-Type: application/json" -d '{"cursus_user":{"begin_at":"'"${start}"'","cursus_id":"'"${cursus_id}"'","end_at":"'"${end}"'","user_id":"'"${user}"'"}}' "https://xxxxxx/xxxxxx.com")

使用 Shellcheck 通常可以在使用 shell 代码时节省大量时间。

这更像是一个格式化的评论。

两个提示:

  • 使用 生成 JSON -- 它会为您获取正确的所有引用
  • 使用数组提高可读性(我不喜欢水平滚动)
data=$(
    jq  --null-input \
        --compact-output \
        --arg begin_at  "$start" \
        --arg cursus_id "$cursus_id" \
        --arg end_at    "$end" \
        --arg user_id   "$user" \
        '{cursus_user: $ARGS.named}'
)

curl_opts=(
    -X POST
    -H "Authorization: Bearer ${bearer_token}"
    -H "Content-Type: application/json" 
    -d "$data"
)

response=$(curl "${curl_opts[@]}" "https://xxxxxx/xxxxxx.com")