无法解析主机 - 传递变量时 cURL 请求无效

无法解析主机 - 传递变量时 cURL 请求无效

我正在尝试使用 shell 脚本,并且我相信我完全按照应有的方式编写了命令,但是,我遇到了奇怪的错误。也许我没有正确地逃避,但我已经尝试过各种事情。

myscript.sh

function seconds_to_readable () {
    num=$1
    min=0
    hour=0
    if((num>59));then
            ((sec=num%60))
            ((num=num/60))
            if((num>59));then
                    ((min=num%60))
                    ((num=num/60))
                    if((num>23));then
                            ((hour=num%24))
                    else
                            ((hour=num))
                    fi
            else
                    ((min=num))
            fi
    else
            ((sec=num))
    fi
    echo "$hour""h "$min"m "$sec"s"
}
ENVIRONMENT=production
runtime=421
DEPLOY_STARTED_AT="2020-08-12 13:09:00"
runtimeYarnInstall="123"
runtimeGenerate="456"
runtimeWrangler="789"
WEBSITE_LINK="https://domain.com"
ERROR_MESSAGE=123
curl -X POST -H 'Content-type: application/json' --data '{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": ":tada: '$ENVIRONMENT' website has been deployed in *'$(seconds_to_readable $runtime)'*" } }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "*Environment:* '$ENVIRONMENT'" }, { "type": "mrkdwn", "text": "*Started at:* '$DEPLOY_STARTED_AT'" }, { "type": "mrkdwn", "text": "*Runtime:* '$(seconds_to_readable $runtime)'" } ] }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "*Yarn install:* '$(seconds_to_readable $runtimeYarnInstall)'" }, { "type": "mrkdwn", "text": "*Generate (modern):* '$(seconds_to_readable $runtimeGenerate)'" }, { "type": "mrkdwn", "text": "*Wrangler publish:* '$(seconds_to_readable $runtimeWrangler)'" } ] }, { "type": "image", "title": { "type": "plain_text", "text": ":camera_with_flash: '$WEBSITE_LINK'", "emoji": true }, "image_url": "https://api.apiflash.com/v1/urltoimage?access_key=keyhere&url='$WEBSITE_LINK'&fresh=true", "alt_text": "website screenshot" } ] }' https://hooks.slack.com/services/x/x/x

输出是:

sh myscript.sh
curl: (6) Could not resolve host: 7m
curl: (3) unmatched close brace/bracket in URL position 6:
1s*" } }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "*Environment:* production" }, { "type": "mrkdwn", "text": "*Started at:* 2020-08-12
    ^
curl: (3) unmatched close brace/bracket in URL position 11:
13:09:00" }, { "type": "mrkdwn", "text": "*Runtime:* 0h
        ^
curl: (6) Could not resolve host: 7m
curl: (3) unmatched close brace/bracket in URL position 5:
1s" } ] }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "*Yarn install:* 0h
    ^
curl: (6) Could not resolve host: 2m
curl: (3) unmatched close brace/bracket in URL position 5:
3s" }, { "type": "mrkdwn", "text": "*Generate (modern):* 0h
    ^
curl: (6) Could not resolve host: 7m
curl: (3) unmatched close brace/bracket in URL position 6:
36s" }, { "type": "mrkdwn", "text": "*Wrangler publish:* 0h
    ^
curl: (6) Could not resolve host: 13m
curl: (3) unmatched close brace/bracket in URL position 5:
9s" } ] }, { "type": "image", "title": { "type": "plain_text", "text": ":camera_with_flash: https://domain.com", "emoji": true }, "image_url": "https://api.apiflash.com/v1/urltoimage?access_key=keyhere&url=https://domain.com&fresh=true", "alt_text": "website screenshot" } ] }

答案1

引用肯定会变得一团糟。首先使用heredoc 将整个 json 对象放入变量中,然后将其传递给curl。

你可以这样做:

read -r -d '' _json <<EOJSON
{ 
    "blocks": [ 
        { 
            "type": "section", 
            "text": { 
                "type": "mrkdwn", 
                "text": ":tada: $ENVIRONMENT website has been deployed in *$(seconds_to_readable $runtime)*" 
                } 
        }, 
        { 
            "type": "context", 
            "elements": [ 
                { 
                    "type": "mrkdwn", 
                    "text": "*Environment:* $ENVIRONMENT" 
                }, 
                { 
                    "type": "mrkdwn", 
                    "text": "*Started at:* $DEPLOY_STARTED_AT" 
                }, 
                { 
                    "type": "mrkdwn", 
                    "text": "*Runtime:* $(seconds_to_readable $runtime)" 
                } 
            ] 
        }, 
        { 
            "type": "context", 
            "elements": [ 
                { 
                    "type": "mrkdwn", 
                    "text": "*Yarn install:* $(seconds_to_readable $runtimeYarnInstall)" 
                }, 
                { 
                    "type": "mrkdwn", 
                    "text": "*Generate (modern):* $(seconds_to_readable $runtimeGenerate)" 
                }, 
                { 
                    "type": "mrkdwn", 
                    "text": "*Wrangler publish:* $(seconds_to_readable $runtimeWrangler)" 
                } 
            ] 
        }, 
        { 
            "type": "image", 
            "title": { 
                "type": "plain_text", 
                "text": ":camera_with_flash: $WEBSITE_LINK", 
                "emoji": true 
            }, 
            "image_url": "https://api.apiflash.com/v1/urltoimage?access_key=keyhere&url=$WEBSITE_LINK&fresh=true", 
            "alt_text": "website screenshot" 
        } 
    ] 
}
EOJSON

curl -X POST -H 'Content-type: application/json' --data "$_json" https://hooks.slack.com/services/x/x/x

相关内容