Bash script curl

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


#!/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`

eval $entireURL

这工作完美!

链接地址: http://www.djcxy.com/p/48578.html

上一篇: 如何自动加载细节

下一篇: Bash脚本卷曲