[英]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"
我想要這樣的腳本:curl -X POST -H“內容類型:application / json” -H“緩存控制:無緩存” -d'{“用戶名”:“ abc@abc.com”,“密碼“:” Password123“}'” https://example.com/session “
整個URL IS: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執行。 因為嵌入在括號中的A命令作為子外殼運行,所以缺少我的環境變量。
#!/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
這很完美!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.