1

Below is my shell script in which I'm trying to use the value of a variable as input to curl. However, the script is giving an error and when trying to debug the script, I can see that the value of variable v_sub_get is partially substituted as input to PATCH curl.

Script:

#!/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": "probe.test20@gmail.com",
                "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

Debug Output:

Part of Debug output where Update profile is invoked:

+ 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

We can see that from Debug output that value of variable "v_sub_get" is partially substituted as input to Curl with PATCH function used in Update_profile function.

Why is the value of the variable "v_get_sub" getting partially substituted when used as input to Curl ?

  • I can't reproduce this. Could there be a non-printing character in your declaration of v_etag? Can you post the actual script file somewhere (or a minimal version of it that reproduces the error) so we can check? – terdon Jun 19 '18 at 11:41
  • Actually, this is just a part of the long shell script. The value of "v_sub_get" is derived from another curl output which is properly displayed. However, when used as input to the curl with PATCH function, it is partially substituted in the script. the value of v_etag is properly substituted. i'll try to add the entire script with debug output. – Mayur Kadam Jun 19 '18 at 11:52
  • 1
    Try and find the smallest set of instructions that reproduces the error. It isn't an issue with what you have shown us here, so we can't help unless you can give us something we can run that has the same error. Start by adding various echo calls everywhere to see the values of your variables at different times. – terdon Jun 19 '18 at 12:10
  • The echo value is included just before the curl where the issue is ocurring – Mayur Kadam Jun 19 '18 at 12:19
  • Does this make any difference? v_sub_get=$(echo $content | jq -r .user_details.sub | sed 's/\r//g') – terdon Jun 19 '18 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.