쉘 스크립트를 사용하려고 하는데 명령을 원래대로 작성했다고 생각하는데 이상한 오류가 발생합니다. 어쩌면 제대로 탈출하지 못했을 수도 있지만 다양한 시도를 해봤습니다.
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