6

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.

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

2
  • 4
    To get the result of the command as a variable at the Command Prompt, you'd use For /F %G In ('%__AppDir__%curl.exe -s -o NUL "https://www.google.com" -w "%{http_code}\n"') Do @Set "var=%G". If you want to do it in a batch file, then using the advice in the for command help information, for /?, which states "To use the FOR command in a batch program, specify %%variable instead of %variable.", you'd do it like this, @For /F %%G In ('%__AppDir__%curl.exe -s -o NUL "https://www.google.com" -w "%%{http_code}\n"') Do @Set "var=%%G". Note: I don't recommend using this domain for any reason Commented Oct 31, 2020 at 1:02
  • In a batch file, the %-symbol in front of {http_code} need to be escaped by doubling it, like %%{http_code} Commented Oct 31, 2020 at 22:30

4 Answers 4

6

Thanks to @Compo, here is the formatted answer for other's benefit.

@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 

Sign up to request clarification or add additional context in comments.

Comments

2

Based upon your own answer code, there's no need to use a to get the result saved to a , and then compare that variable value with a known value. You can pipe the result through to see if it matches your known value instead.

Example :

@%__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 :

%__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 :

@(%__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

But you could have still done it like this, without creating the variable.

@Echo Off
SetLocal EnableExtensions
For /F %%G In ('%__AppDir__%curl.exe -s -o NUL "www.google.com"
 -w "%%{http_code}\n"') Do (Echo response code is %%G
    If %%G Equ 200 (Echo Able to ping Google.) Else Echo Unable to ping Google.
)
Pause

Comments

1

As he has mentionned above @Compo in his comment :

"To get the result of the command as a variable at the Command Prompt, you'd use 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

Comments

-1

Other answers are good, but I would not use CURL to just test if we can reach a site:

@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
)

1 Comment

The question is how to store curl command output in a bash script, not how to see if a site is up. In any case, ping only tests layer 3 connectivity via ICMP. Curl tests layer 7 connectivity via HTTP(S) and can be used to confirm a website is responding (which can be used to verify the full stack is operating - load balancer, web server, application server, etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.