ホストを解決できない - 変数を渡すときに誤ったcURL要求

ホストを解決できない - 変数を渡すときに誤ったcURL要求

シェルスクリプトを使用しようとしていますが、コマンドを元に書き込んだと思いますが、奇妙なエラーが発生します。たぶんちゃんと脱出できなかったかもしれませんが、いろいろ試してみました。

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オブジェクト全体を変数に入れ、それをカールに渡します。

次のことができます。

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

関連情報