在变量中存储命令的输出,其中命令已经在其自己的变量中执行/存储

发布于 2025-02-10 23:26:15 字数 473 浏览 0 评论 0原文

我有一个存储在变量中的命令。我想执行该命令,然后将该命令的响应存储在变量中。我对响应的语法错误有问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2025-02-17 23:26:15

您正在尝试执行一个名为{CommandVar}的程序,并且该程序在您的路径中不存在。

首先,您最好使用一个数组来存储您的命令,每个数组元素持有一个命令参数:

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"}')

这样,您可以通过

"${commandVar[@]}"

分别将其standardOutput存储到这样的变量中来执行它:

responseVar=$( "${commandVar[@]}" )

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:

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"}')

With this, you can execute it by

"${commandVar[@]}"

respectively store its standardoutput into a variable like this:

responseVar=$( "${commandVar[@]}" )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文