Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

We are testing our server.

I am a beginner to shell scripting.

I need an bash shell script, to read a text file having URLs, query each URL to the server and check whether the application server returns a 200 OK or 302 Redirect response.

Can you point me to any tutorial or link to do this ?

share|improve this question

3 Answers 3

for name in `cat textfile`; do curl -s -o /dev/null --write-out %{http_code} $name  2>&1 ; echo -e "\n"; done

the explanation:

for url in `cat textfile`

Output all the entries in your text file with the cat command, and make the current pointer available through the url variable.

curl -s -o /dev/null --write-out %{http_code} $url 2>&1

Call cURL and suppress any progress status with -s, write any other output to /dev/null (it's a black hole which you can throw anything into) with the -o switch, then use --write out %{http_code} to give you the return code for each link you do, put the current link down with $url, and redirect STDERR to STDOUT with 2>&1 (in case you do get any errors). Then...

; echo -e "\n"

Echo on a new line, regardless if the last statement (that whole cURL block) failed.

`; done

Finish the loop.

This probably isn't what you wanted though, since it only prints out the return codes.

share|improve this answer

Use wget command with --spider option. However this may cause HEAD request instead of GET which, depending on web site configuration may cause incorrect results. Another alternative is to use curl.

share|improve this answer

Follow-up to @qweet

Use curl with -K, --config <config file> command-line parameter. Each URL in this config-file must be defined with url parameter:

url="page/to/get"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.