在变量中存储命令的输出,其中命令已经在其自己的变量中执行/存储
我有一个存储在变量中的命令。我想执行该命令,然后将该命令的响应存储在变量中。我对响应的语法错误有问题。
commandVar="curl --location -g --request POST 'https://172.217.25.35/rest-gateway/rest/api/v1/auth/token' --header 'Content-Type: application/json' --header 'Authorization: Basic U0FNcWE6NTYyMFNhbSE=' --data-raw '{"grant_type": "client_credentials"}'"
responseVar=$("{commandVar}")
echo "Response of command: ${responseVar}"
有人知道我将如何做的正确的语法吗?我最终需要解析命令的输出,这就是为什么我需要将响应存储在其自身变量中的原因。
I have a command that is stored in a variable. I would like to execute that command and then store the response of that command in a variable. I'm having an issue with syntax errors for the responseVar=$("{commandVar}") line as it seems to trigger a "no such file or directory" error instead of actually running the command/storing the output in the response variable.
commandVar="curl --location -g --request POST 'https://172.217.25.35/rest-gateway/rest/api/v1/auth/token' --header 'Content-Type: application/json' --header 'Authorization: Basic U0FNcWE6NTYyMFNhbSE=' --data-raw '{"grant_type": "client_credentials"}'"
responseVar=$("{commandVar}")
echo "Response of command: ${responseVar}"
Does anyone know the correct syntax on how I would do this? I need to eventually parse the output of the command which is why I need to store the response in it's own variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试执行一个名为
{CommandVar}
的程序,并且该程序在您的路径中不存在。首先,您最好使用一个数组来存储您的命令,每个数组元素持有一个命令参数:
这样,您可以通过
分别将其standardOutput存储到这样的变量中来执行它:
You are trying to execute a program named
{commandVar}
, and this program does not exist in your PATH.To start with, you better use an array to store your command, each array element holding one command argument:
With this, you can execute it by
respectively store its standardoutput into a variable like this: