[英]Store curl command output in a variable in batch script
I tried many examples online, but was not able to make it work.我在网上尝试了很多示例,但无法使其正常工作。 I am new to this so please forgive my primitive question.
我是新手,所以请原谅我的原始问题。
say I have this:说我有这个:
curl "www.google.com" -o /dev/null -s -w "%{http_code}\n"
when running this in a cmd window, it returns the response code.在 cmd 窗口中运行它时,它会返回响应代码。
200
which is as expected.正如预期的那样。
I would like to do something like this in a batch script, but also use the output of the curl command to set a variable.我想在批处理脚本中做这样的事情,但也使用 curl 命令的输出来设置一个变量。 All examples I found online propose:
我在网上找到的所有例子都提出:
set res = $(curl "www.google.com" -o /dev/null -s -w "%{http_code}\n")
echo %res%
pause
or like this或者像这样
FOR /F "tokens=* USEBACKQ" %%F IN (`curl www.google.com -o /dev/null -s -w "%{http_code}\n"`) DO ( SET var=%%F )
ECHO %var%
pause
both did not work, my batch script closes immediately.两者都不起作用,我的批处理脚本立即关闭。
Any feedback or help is highly appreciated thank you.非常感谢任何反馈或帮助,谢谢。
As he has mentionned above @Compo in his comment :正如他在上面@Compo 的评论中提到的:
"To get the result of the command as a variable at the Command Prompt, you'd use For /F
" “要在命令提示符处将命令结果作为变量,您可以使用
For /F
”
@echo off
Title Store curl command output in a variable in batch script
Set "MyCommand=curl "www.google.com" -o NUL -s -w "%%{http_code}""
@for /f %%R in ('%MyCommand%') do ( Set VAR=%%R )
echo %VAR%
pause
Thanks to @Compo, here is the formatted answer for other's benefit.感谢@Compo,这是为了他人的利益而格式化的答案。
@echo off
For /F %%G In ('%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%%{http_code}\n"') Do Set "response=%%G"
echo response code is %response%
IF %response% == 200 (
ECHO was able to ping google
) ELSE (
ECHO unable to ping google
)
pause
Other answers are good, but I would not use CURL to just test if we can reach a site:其他答案很好,但我不会使用 CURL 来测试我们是否可以访问某个站点:
@echo off
ping google.com -n 1 >nul 2>&1
if %errorlevel% equ 1 (
echo EHHH..NO Google is not down, we are
) else (
echo HURRAH MAN We are OK
)
Based upon your own answer code, there's no need to use a for-loop to get the result saved to a variable , and then compare that variable value with a known value.根据您自己的答案代码,无需使用for 循环将结果保存到变量中,然后将该变量值与已知值进行比较。 You can pipe the result through findstr to see if it matches your known value instead.
您可以通过findstr管道结果以查看它是否与您的已知值匹配。
Example batch-file :示例批处理文件:
@%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%%{http_code}" ^
| %__AppDir__%findstr.exe /X "200" 1> NUL && (Echo Ping Succeeded
) || Echo Ping Failed
@Pause
It is only really one line, split for readability, so you could do it line this in cmd :它实际上只有一行,为了可读性而拆分,因此您可以在cmd 中执行此操作:
%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%{http_code}" | %__AppDir__%findstr.exe /X "200" 1> NUL && (Echo Ping Succeeded) || Echo Ping Failed
Or like this as a batch-file :或者像这样作为批处理文件:
@(%__AppDir__%curl.exe -s -o NUL "www.google.com" -w "%%{http_code}" | %__AppDir__%findstr.exe /X "200" 1> NUL && (Echo Ping Succeeded) || Echo Ping Failed) & Pause
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.