-1

I'm trying to check if a file in the url exists or not using wget, but the result it is giving is not as expected.

It is giving the result as Spider mode enabled. Check if remote file exists.

The command I'm using is:

cd $path

if [ $? -eq 0 ]; then
    touch laber.txt
    if [ $? -eq 0 ]; then
        echo $key >> laber.txt
        if [ $? -eq 0 ]; then
            wget -S --spider $url/laber.txt 2>&1 | grep -q 'HTTP/1.0 200 OK'
            if [ $? -eq 0 ]; then
                echo OK;
            else
                rm -r laber.txt
                echo FAIL;
            fi
        else
            echo 'test';
        fi
    else
      echo '3';
    fi
else
    echo '4';
fi
Inian
  • 80,270
  • 14
  • 142
  • 161
Fazeela Abu Zohra
  • 641
  • 1
  • 12
  • 31

1 Answers1

1

Simple way to do it. Idea is to grep on the header output from wget on the requested URL and get the return code of the operation as such.

grep with -q will operate in silent mode i.e. will not output search string into stdout in case it is found.

A fancy one-liner could do the trick

#!/bin/bash

wget -S --spider $1 2>&1 | grep -q 'HTTP/1.0 200 OK' && echo SUCCESS || echo FAIL

Confirmation:-

$ ./script.sh www.go3463tgogle.com
FAIL
$ ./script.sh www.google.com
SUCCESS

Traditional way:-

#!/bin/bash

wget -S --spider $1 2>&1 | grep -q 'HTTP/1.0 200 OK'

if [ $? -eq 0 ]; then
    echo OK
else
    echo FAIL
fi

Confirmation:-

$ ./script.sh www.go3463tgogle.com
FAIL
$ ./script.sh www.google.com
OK

You can use any actual url with the script to adopt for your case.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • I've updated the code, can you please look into the code – Fazeela Abu Zohra May 20 '16 at 12:49
  • Why do you have to check return codes of `echo`, `touch`? You can remove those checks and have only for `wget`. Also please improve the formatting of the script – Inian May 20 '16 at 12:52
  • Your script has bunch of unused variables `$key` `$url` are accessed without having a value. If you are ever feeling doubtful of the script, copy paste your script in https://www.shellcheck.net/ fix the errors – Inian May 20 '16 at 12:55
  • I'm passing $key and $url from php code to script. What I'm trying to do is I've a url sample.com/book.txt, if that particular file exist in that url, then it should return OK, else return FAIL... Any idea on how to do this?? – Fazeela Abu Zohra May 23 '16 at 04:41