将cURL和jq输出保存到批处理脚本中的变量

时间:2019-03-10 08:25:10

标签: batch-file curl jq

我使用此命令来检索Jenkins中的构建结果:

curl -s  http://<jenkins_url>/job/<job_name>/lastCompletedBuild/api/json | jq ."result"

基于该结果,我需要决定要使用批处理命令执行的操作,

如何将命令的输出另存为变量?

1 个答案:

答案 0 :(得分:1)

您应该能够在批处理文件中使用For /F命令将命令的输出返回到变量:

For /F "Delims=" %%A In ('"curl -s  http://<jenkins_url>/job/<job_name>/lastCompletedBuild/api/json | jq ."result""') Do Set "test=%%~A"
If /I "%test%"=="failed" ...DoSomething
If /I "%test%"=="success" ...DoSomethingElse
If /I "%test%"=="unstable" ...DoAnotherThing

但是,根本不需要设置变量,因为您可以直接使用返回的元变量:

For /F "Delims=" %%A In ('
    "curl -s  http://<jenkins_url>/job/<job_name>/lastCompletedBuild/api/json | jq ."result""
') Do (
    If /I "%%~A"=="failed" ...DoSomething
    If /I "%%~A"=="success" ...DoSomethingElse
    If /I "%%~A"=="unstable" ...DoAnotherThing
)
相关问题