我必须努力寻找一切可能的办法,在指挥要求中处理枪匹马, 请看以下法典,看一看如果使用不当,为什么使用单一报价会失败。
# Create Release and Tag commit in Github repository
# returns string with in-place substituted variables
json=$(cat <<-END
{
"tag_name": "${version}",
"target_commitish": "${branch}",
"name": "${title}",
"body": "${notes}",
"draft": ${is_draft},
"prerelease": ${is_prerelease}
}
END
)
# returns raw string without any substitutions
# single or double quoted delimiter - check HEREDOC specs
json=$(cat <<-!"END" # or END
{
"tag_name": "${version}",
"target_commitish": "${branch}",
"name": "${title}",
"body": "${notes}",
"draft": ${is_draft},
"prerelease": ${is_prerelease}
}
END
)
# prints fully formatted string with substituted variables as follows:
echo "${json}"
{
"tag_name" : "My_tag",
"target_commitish":"My_branch"
....
}
http://www.un.org/Depts/DGACM/index_french.htm
# enclosing in single quotes means no variable substitution
# (treats everything as raw char literals)
echo ${json}
${json}
echo "${json}"
"${json}"
# enclosing in single quotes and outer double quotes causes
# variable expansion surrounded by single quotes(treated as raw char literals).
echo " ${json} "
{
"tag_name" : "My_tag",
"target_commitish":"My_branch"
....
}
www.un.org/Depts/DGACM/index_spanish.htm 说明2:与线路终点站保持距离
- Note the json string is formatted with line terminators such as LF
- or carriage return
(if its encoded on windows it contains CRLF
)
- using (translate)
tr
utility from shell we can remove the line terminators if any
# following code serializes json and removes any line terminators
# in substituted value/object variables too
json=$(echo "$json" | tr -d
| tr -d
)
# string enclosed in single quotes are still raw literals
echo ${json}
${json}
echo "${json}"
"${json}"
# After CRLF/LF are removed
echo " ${json} "
{ "tag_name" : "My_tag", "target_commitish":"My_branch" .... }
说明3:格式
- while manipulating json string with variables, we can use combination of
and "
such as following, if we want to protect some raw literals using outer double quotes to have in place substirution/string interpolation:
# mixing and "
username=admin
password=pass
echo "$username:$password"
admin:pass
echo "$username" : "$password"
admin:pass
echo "$username" [${delimiter}] "$password"
admin[${delimiter}]pass
www.un.org/Depts/DGACM/index_chinese.htm
- Following curl request already removes existing
(ie serializes json)
response=$(curl -i
--user ${username}:${api_token}
-X POST
-H Accept: application/vnd.github.v3+json
-d "$json"
"https://api.github.com/repos/${username}/${repository}/releases"
--output /dev/null
--write-out "%{http_code}"
--silent
)
So when using it for command variables, validate if it is properly formatted before using it :)