can I use WGET to get, let's say, status 200 OK and save that status somewhere? If not, how can I do that using ubuntu linux?
Thanks!
CodePudding user response:
With curl you can
curl -L -o /dev/null -s -w "%{http_code}\n" http://google.com >> status.txt
CodePudding user response:
You use --save-headers
to add the headers to the output, put the output to the console using -O -
, ignore the errors stream using >/dev/null
and get only the status line using grep HTTP/
.
You can then output that into a file using >status_file
$ wget --save-headers -O - http://google.com/ 2>/dev/null | grep HTTP/ > status_file
CodePudding user response:
The question suggests that the output of the wget command be stored somewhere. As another alternative, the following example shows how to store the output of wget execution in a shell variable (wget_status). Where after the execution of the wget command the status of the execution is stored in the variable wget_status. The wget status is displayed in the console using the echo command.
$ wget_status=$(wget --server-response ${URL} 2>&1 | awk '/^ HTTP/{print $2}')
$ echo $wget_status
200
After the execution of the wget command, the execution status can be manipulated using the value of the wget_status variable.
For more information consult the following link as a reference:
https://www.unix.com/shell-programming-and-scripting/148595-capture-http-response-code-wget.html
The tests were executed using cloudshell on a linux system.
Linux cs-335831867014-default 5.10.90 #1 SMP Wed Mar 23 09:10:07 UTC 2022 x86_64 GNU/Linux