Bash脚本卷曲

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

我想我的脚本是这样的:curl -X POST -H“Content-Type:application / json”-H“Cache-Control:no-cache”-d'{“username”:“abc@abc.com”,“密码“:”Password123“}'”https://example.com/session“

整个URL是:curl -H“Content-Type:application / json”-H“Cache-Control:no-cache”-d'{“username”:“zzzzzz”,“password”:“azzzsass”}'https: //cloud.tenable.com/session

但它不在bash中执行。 它给了我这个错误:

{"statusCode":400,"error":"Bad Request","message":"child "username" fails because ["username" is required]","validation":{"source":"payload","keys":["username"]}}

但它不起作用。 谁能帮我?

更新


我自己解决了它。 一切都是正确的,除了执行作为eval $ entireURL。 由于嵌入在圆括号内的命令作为子外壳运行,因此我的环境变量缺失。


#!/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/48577.html

上一篇: Bash script curl

下一篇: Keyboard Maestro Bash CURL Script Works Without Variable But Not With One