下面是我的 shell 脚本,我在其中尝试使用变量的值作为curl
.但是,脚本给出了错误,当尝试调试脚本时,我可以看到变量的值v_sub_get
被部分替换为 PATCH 的输入curl
。
脚本:
#!/bin/bash
PATH=/opt/SP/apps/jq/:/opt/SP/jboss/home/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
export PATH
function Create_Profile {
content=$(curl -X POST -D /var/SP/data/monitoring/probes/create_profile_headers.txt -sS https://somedomain/backend/profile/ -H 'accept: application/json' -H 'content-type: application/json' -d '{"user_details": {
"name": "myname",
"given_name": "Diego",
"family_name": "Maradona",
"middle_name": "Armando",
"preferred_username": "pibe de oro",
"nickname": "masterofuniverse",
"profile": "http://www.god.com",
"picture": "http://www.handsome.com/diego.gif",
"website": "http://www.god.com",
"email": "[email protected]",
"email_verified": "true",
"gender": "male",
"zoneinfo": "CET",
"locale": "AR",
"phone_number": "493333333333",
"phone_number_verified": true,
"address": {
"street_address": "6767 Collins Ave",
"locality": "Miami Beach",
"region": "FL",
"postal_code": "33141",
"country": "DE"
}
},
"device_details": {
"user_agent": "iOS",
"device_id": "123123123123123123",
"ip_address": "1.2.3.4"
},
"security_details": {
"password": "India@1234",
"security_questions": [{
"question": "Who is God?",
"answer": "me"
}]
}
}')
v_sub_get=`echo $content | jq -r .user_details.sub`
echo "Sub value for Profile created: $v_sub_get"
http_response=`cat /var/SP/data/monitoring/probes/create_profile_headers.txt | awk -F " " '/HTTP\/1.1 / {print$2}'`
v_etag=`cat /var/SP/data/monitoring/probes/create_profile_headers.txt | awk -F " " '/ETag/ {print$2}'`
#echo "Create Profile invoked"
echo "ETag value: $v_etag"
http_response_create=`echo $http_response|awk {print$2}`
echo "http_response_create: $http_response_create"
dt=$(date '+%d/%m/%Y %H:%M:%S')
if [ "$http_response_create" == "100 201" ]
then echo "$dt Success Create Profile is working Fine. http_response_create: $http_response_create" >> /var/SP/data/monitoring/probes/Probe_logs.txt
create_exit_code=0
else echo "$dt Error Create Profile has some Issue. http_response_create: $http_response_create" >> /var/SP/data/monitoring/probes/Probe_logs.txt
create_exit_code=2
fi
}
################ Update Profile for created user #####################
function Update_Profile {
#echo "Update Profile Invoked"
echo "Sub_value_patch: $v_sub_get"
patch_content=$(curl -X PATCH -D /var/SP/data/monitoring/probes/update_profile_headers.txt \
-sS "https://somedomain/backend/profile/${v_sub_get}" \
-H 'content-type: application/merge-patch+json' \
-H "if-none-match: ${v_etag}" \
-d '{"user_details": {"name": "MyNewName"}}' )
http_response_update=`cat /var/SP/data/monitoring/probes/update_profile_headers.txt| awk '/HTTP/ {print $2}'`
echo "http_response_update: $http_response_update"
v_updated_name=`echo $patch_content | jq -r .user_details.name`
echo "Updated Name: $v_updated_name"
dt=$(date '+%d/%m/%Y %H:%M:%S')
if [ "$http_response_update" -eq 201 ] && [ "$v_updated_name" == "MyNewName" ]
then echo "$dt Success Update Profile is working Fine. http_response_update: $http_response_update" >> /var/SP/data/monitoring/probes/Probe_logs.txt
update_exit_code=0
else echo "$dt Error Update Profile has some Issue. http_response_update: $http_response_update" >> /var/SP/data/monitoring/probes/Probe_logs.txt
update_exit_code=2
fi
#echo $patch_content | jq -r .user_details.name
}
############## Delete Profile for User created ####################
function Delete_Profile {
delete_content=$(curl -X DELETE -D /var/SP/data/monitoring/probes/delete_profile_headers.txt -sS https://somedomain/backend/profile/$v_sub_get )
delete_response=`cat /var/SP/data/monitoring/probes/delete_profile_headers.txt | awk -F " " '/HTTP/ {print$2}'`
echo "http_response_delete: $delete_response"
dt=$(date '+%d/%m/%Y %H:%M:%S')
if [ $delete_response -eq 200 ]
then
echo "$dt Success Delete profile is working fine. http_response_delete: $delete_response" >> /var/SP/data/monitoring/probes/Probe_logs.txt
delete_exit_code=0
else
echo "$dt Error Delete profile has some issue. http_response_delete: $delete_response" >> /var/SP/data/monitoring/probes/Probe_logs.txt
delete_exit_code=2
fi
}
############# Get Profile of User ###############
function Get_Profile {
get_content=$(curl -X GET -D /var/SP/data/monitoring/probes/get_profile_headers.txt -sS 'https://somedomain/backend/profile/lookup?qemail=probe.test20%40gmail.com&qstatus=active' -H 'accept: application/json' -H 'content-type: application/json')
v_sub_get=`echo $get_content | jq -r '.items[].sub'`
echo $v_sub_get
dt=$(date '+%d/%m/%Y %H:%M:%S')
get_response=`cat /var/SP/data/monitoring/probes/get_profile_headers.txt | awk -F " " '/HTTP/ {print$2}'`
if [ $get_response -eq 200 ]
then
echo "http_response_get: $get_response"
echo "$dt Success Get profile is working Fine. http_response_get: $get_response" >> /var/SP/data/monitoring/probes/Probe_logs.txt
if [ -z "$v_sub_get" ]
then
echo "User profile does not exists.Creating the user profile."
Create_Profile
Update_Profile
Delete_Profile
else
echo "User profile exists"
Delete_Profile
Create_Profile
Update_Profile
Delete_Profile
fi
else
echo "$dt Error Get profile has some issue. http_response_get: $get_response" >> /var/SP/data/monitoring/probes/Probe_logs.txt
echo "http_response_get: $get_response"
exit 2
fi
}
v_sub_get=""
v_etag=""
create_exit_code=""
update_exit_code=""
delete_exit_code=""
Get_Profile
dt=$(date '+%d/%m/%Y %H:%M:%S')
if (( $create_exit_code == 0 && $update_exit_code == 0 && $delete_exit_code == 0 ))
then
echo "$dt NV Probe 0" >> /var/SP/data/monitoring/probes/CRM_Probe_exit_status.log
exit 0
else
echo "$dt NV Probe 2" >> /var/SP/data/monitoring/probes/CRM_Probe_exit_status.log
exit 2
fi
调试输出:
调用更新配置文件的调试输出的一部分:
+ Update_Profile
+ echo 'Sub_value_patch: 2790ca3e-74ee-486a-b991-6bf2dd4c6afb'
Sub_value_patch: 2790ca3e-74ee-486a-b991-6bf2dd4c6afb
++ curl -X PATCH -D /var/SP/data/monitoring/probes/update_profile_headers.txt -sS https://somedomain/backend/profile/2790ca3e-74ee-486a-' -d '{"user_details": {"name": "MyNewName"}}'n/merge-patch+json' -H 'if-none-match: 605434da-8e87-4b66-8397-c452b106a23a
+ patch_content='<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>'
++ cat /var/SP/data/monitoring/probes/update_profile_headers.txt
++ awk '/HTTP/ {print $2}'
+ http_response_update=400
+ echo 'http_response_update: 400'
http_response_update: 400
我们可以从调试输出中看到,变量“v_sub_get”的值被 Update_profile 函数中使用的 PATCH 函数部分替换为 Curl 的输入。
为什么变量“v_get_sub”的值在用作 Curl 的输入时被部分替换?