Bash 脚本卷曲

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48576683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:48:32  来源:igfitidea点击:

Bash script curl

bashshellcurl

提问by tech_enthusiast

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

我想要这样的脚本: curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"username":"[email protected]", "密码":"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

整个 URL 是: 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:

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

{"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.

我自己解决了。一切都是正确的,除了作为 eval $entireURL 执行。因为括号内嵌入的命令作为子shell运行,所以我的环境变量丢失了。

回答by tech_enthusiast

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

This works perfect !

这工作完美!