Here's some of the lines from my shell script:
curl -I --connect-timeout 60 google.com|grep "Server:" >> 1000-server.txt
curl -I --connect-timeout 60 yahoo.com|grep "Server:" >> 1000-server.txt
curl -I --connect-timeout 60 bing.com|grep "Server:" >> 1000-server.txt
My script is getting the web server type correctly in most cases:
Server: gws
Server: ATS
Server: Microsoft-IIS/8.5
However, if the curl connection times out or get an error, it doesn't output any message. Also, if for some reason grep doesn't find anything, it doesn't output any error message.
If curl or grep fails, is there any way to output an error message that just says something like "ERROR: NOT FOUND"?
Sorry if this is a newbie questions, just can't figure it out...
curl -I --connect-timeout 60 google.com|grep "Server:" >> 1000-server.txt || echo ERROR | tee -a 1000-server.txt
The curl ... | grep ... command will return false if grep doesn't find the string. In that case, the || echo ERROR | tee -a 1000-server.txt will execute.
The output from echo is piped into tee, which, in turn, will append (-a) the output to the file 1000-server.txt also printing the same output to STDOUT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With