我有一个bash函数,该函数调用curl命令执行一些jenkins操作(删除搁置的项目)
function get_data {
local user="${1}" # user
local api_token="${2}" # token
local http_method="${3}" # POST
local rest_call_url="${4}"
local other_curl_opt="${5}" # -H "Content-Type:application/json" -H "X-Api:asdhds890asd"
# encode space characters
rest_call_url=$(echo "$rest_call_url" | sed 's/ /%20/g')
# gets data
data=$(curl -u ${user}:${api_token} -k -s ${other_curl_opt} -X $http_method "${rest_call_url}")
return_status=$?
if [[ "${data}" = *"HTTP Status 404"* ]] || [[ "${data}" = *"HTTP ERROR 404"* ]] || [[ "${data}" = *"Bad parameter"* ]] || [ $return_status -ne 0 ]
then
print_log ${job_name} ERROR "Curl command failed"
else
print_log ${job_name} INFO "Curl command successful"
fi
}
在传递了必需的参数后,又在另一个函数中调用了该函数
这是函数调用
jenkins_operation_url=https://${jenkins_instance}/shelvedProjects/manageShelvedProject/
opts="--data \"projects=${job_name}\" --data \"delete=\""
get_data "${jenkins_user}" "${api_token}" "POST" "${jenkins_operation_url}" "${opts}"
现在的问题是,调用此函数时,curl调用中会添加一些额外的字符
样本数据为
curl -u user:xxxxxxxxxxxxxxx -k -s --data '"projects=xxxxa"' --data '"delete="' -X POST https://xxxxxxxx/shelvedProjects/manageShelvedProject/
+ data='Bad parameter. Should be '\''unshelve'\'' or '\''delete'\'' only.'
我不确定这些多余的字符(单引号)是从何处添加的
--data '"projects=xxxxa"' --data '"delete="'
有人可以帮助我更正我的代码吗
答案 0 :(得分:0)
为变量赋值时,除此类数字外,应始终使用引号:
jenkins_operation_url="https://${jenkins_instance}/shelvedProjects/manageShelvedProject/"