Bash脚本将变量插入JSON负载(松弛Webhook)问题

时间:2019-03-22 23:20:48

标签: json bash variables

我正在尝试将变量插入json有效负载(在shell脚本中工作),但不确定如何正确转义字符

我尝试了许多不同的转义方法,但是我对此一无所知,我要么返回原义字符串,要么不运行

SLACK_ALERT_WEBHOOK=desiredurl

curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
        {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Slack post failed for webhook, please investigate: $SLACK_ALERT_WEBHOOK"
                    }
                }
            ]
        }
    ]}' "$SLACK_ALERT_WEBHOOK"

我只想将SLACK_ALERT_WEBHOOK的值插入到代码“文本”的这一部分中:“ Webhook的松弛发布失败,请调查:$ SLACK_ALERT_WEBHOOK,但它无法运行或返回原义字符串。我有底部的“ $ SLACK_ALERT_WEBHOOK”在底部成功运行,可以发送到所需的备用频道,因此我不必担心。

多亏了三胞胎,我才可以正常工作

curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"*Verification*\", \"attachments\": [{\"blocks\": [{\"type\": \"section\",\"text\": {\"type\": \"mrkdwn\",\"text\": \"$SLACK_ALERT_WEBHOOK\"}}]}]}" $SLACK_ALERT_WEBHOOK

1 个答案:

答案 0 :(得分:-1)

尝试一下:

SLACK_ALERT_WEBHOOK=desiredurl

$(curl -X POST -H 'Content-type: application/json' --data '{"text": "*Daily Webhook Verification*", "attachments": [
        {
            "blocks": [
                {
                    "type": "section",
                    "text": {
                        "type": "mrkdwn",
                        "text": "Slack post failed for webhook, please investigate: `echo $SLACK_ALERT_WEBHOOK`"
                    }
                }
            ]
        }
    ]}' "`echo $SLACK_ALERT_WEBHOOK`")

使用echo <<variable>>只需将其作为命令运行。我在自己的服务器上对其进行了测试,但在您的服务器上可能会有所不同。希望对您有所帮助。