0

What I want to do and what is happening: I want to run a CURL request within my (first) shell script which is currently only working if I hardcode everything.

As soon as I'm using variables, the target responds with an error message.

The respective lines:

This works:

curl -X GET 'https://[the target]' [some headers] -d '{"strObjectType": "ABC","strSearchPattern": "DEF*"}' -k

This works not:

curl -X GET 'https://[the target]' [some headers] -d '{"strObjectType": "${OBJTYPE}","strSearchPattern": "${SEARCH}"}' -k

The command which is used to execute the script:

./abc.sh ABC DEF*

Here the complete script:

#!/bin/sh

#Getting Input
if [ -z "$1" ]; then
  echo "Please supply an object type as first parameter!"
  exit 1
fi
OBJTYPE="$1"

if [ -z "$2" ]; then
  echo "Please supply a search string as second parameter!"
  exit 1
fi
SEARCH="$2"

echo "+++Executing HTTP-REST-Request+++"
#--------------DEBUGGING SECTION START
echo "+++Parameter+++"
echo "Object Type: \"${OBJTYPE}\""
echo "Search Parameter: \"${SEARCH}\""
echo "curl -X GET  'https://[the target]' [some headers] -d '{\"strObjectType\": \"${OBJTYPE}\",\"strSearchPattern\": \"${SEARCH}\"}' -k"
#--------------DEBUGGING SECTION END
echo "+++Result+++"
#Executing HTTP-REST-Request to get Physical Host Data
#curl -X GET '[the target]' [some headers] -d '{"strObjectType": "ABC","strSearchPattern": "DEF*"}' -k
curl -X GET '[the target]' [some headers] -d '{"strObjectType": "${OBJTYPE}","strSearchPattern": "${SEARCH}"}' -k

echo "+++Execution Successful+++"

The output from the target:

Cannot read property '0' of undefined

The content of the two lines must be different in any way. Does anybody know what the problem is?

  • 1
    variables will not get interpolated if used inside single quotes, they would be treated as literal string. – P.... Oct 06 '20 at 16:49
  • Oops! You mean that: "OBJTYPE='$1'"? I'm sorry, that was a mistake which I forgot to change back from my last try to solve the problem. Thank you very much for the hint, but there must be another mistake left. I edited my question and corrected that. – user14402002 Oct 06 '20 at 20:55
  • how about `'{"strObjectType": "${OBJTYPE}","strSearchPattern": "${SEARCH}"}'` this entire string is inside single quotes. – P.... Oct 06 '20 at 21:10
  • Thank you SO much! I changed everything into double quotes and now it works. `"{\"strObjectType\": \"${OBJTYPE}\",\"strSearchPattern\": \"${SEARCH}\"}"` – user14402002 Oct 07 '20 at 08:04
  • It confused me that the echo in the debugging section showed the correct variable contents, although they are in single quotes here as well. – user14402002 Oct 07 '20 at 08:10
  • Glad it worked. Remember the gist. Single quotes prevent variables to expand. – P.... Oct 07 '20 at 14:50

0 Answers0