#!/bin/bash
read -p 'Username: ' uservar
read -p 'Password: ' passvar
cacheVariable1="\"Content-Type:application/json"\"
cacheVariable2="\"Cache-Control:no-cache"\"
parametersVariable="'{\"username\":\"$uservar\",\"password\":\"$passvar\"}'"
echo $parametersVariable
echo $cacheVariable1 $cacheVariable2
websiteVariable="https://example.com/session"
echo $websiteVariable
entireURL="curl -X POST -H "$cacheVariable1" -H "$cacheVariable2" -d "$parametersVariable" "$websiteVariable""
echo "Entire URL IS: $entireURL"
result=`$entireURL`
echo "$result"
I want my script like this: curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"username":"[email protected]", "password":"Password123"}' "https://example.com/session"
Entire URL IS: curl -H "Content-Type:application/json" -H "Cache-Control:no-cache" -d '{"username":"zzzzzz","password":"azzzsass"}' https://cloud.tenable.com/session
But it does not execute in the bash. It gives me this error:
{"statusCode":400,"error":"Bad Request","message":"child \"username\" fails because [\"username\" is required]","validation":{"source":"payload","keys":["username"]}}
But it does not work. Can anyone help me?
Update
I solved it on my own. Everything was correct, except for executing as eval $entireURL. Because A command embedded within a parenthesis runs as a sub-shell so my environment variables were missing.
-dparameter, but I'd have to see what you tried to be specific. In any case, putting it in a variable doesn't help any, it just adds another level of confusion. I'd post another question with the non-variable version.