Gary
Gary

Reputation: 129

Use wget in shell script to test connectivity

I have a list of URLs in urls.txt that I want to test connectivity to from a server.

urls.txt:

www.google.com
www.yahoo.com
www.gmail.com
https://stackoverflow.com/questions

I want to run a simple shell script to test connectivity for each URL, and write the output of wget to a file wget.log I also need to turn off downloads using --spider

This is my script:

urls=( $(awk '/URLs:/{y=1;next}y' urls.txt) )

for url in "${urls[@]}"
do
    wget --spider -o wget.log "${url}"
    if [ grep -q 'connected' wget.log ]; then
            echo 'successful'
    else
            echo 'unsuccessful'
    fi
    rm wget.log
done

The for loop works in that it stores each URL properly in the variable url but I am getting the following error: line 6: [: too many arguments.

Upvotes: 0

Views: 1909

Answers (1)

oliv
oliv

Reputation: 13249

You don't need the [ ... ] in your if statement, just use

...
if grep -q 'connected' wget.log; then
...

That way the if statement looks at the return value of grep.

Upvotes: 1

Related Questions