LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   GNU wget return codes for shell script (http://www.linuxquestions.org/questions/programming-9/gnu-wget-return-codes-for-shell-script-178783/)

greenhornet 05-06-2004 11:49 PM

GNU wget return codes for shell script
 
Hi All,

I'm trying to use GNU wget in a bash shell script to automate the download of virus identities to my linux webserver which will then in turn be picked up by my windows clients. I am using wget timestamping to only download the file if it has changed on the vendors site, I also need to make a copy of this file into a standard name that all my windows clients can use.

At the moment I have;

Code:

#!/bin/sh

if `wget -N http://www.sophos.com/downloads/ide/380_ides.exe`; then
      cp -f 380_ides.exe ides.exe
      echo "IDEs were updated."
else
      echo "IDEs do not need to be updated."
fi

However in the instance where wget does not download the 380_ides.exe file because it is no newer that the one it downloaded last time it still evaluates the "if" and remakes the ides.exe file. My windows clients are also using wget and so they are always thinking the file ides.exe is new and as such are always hammering my webserver for updates they don't need.

I cannot find any documentation on wget return codes on the GNU site. Does anyone know how I can make this work?

thanks heaps,
GH

jschiwal 05-07-2004 01:56 AM

It seems that the return code doesn't indicate whether the file needs to be update or not. But you can test the timestamps of the two files on your drive. I tested the code below and it worked.

Code:

#!/bin/bash

wget -N http://www.sophos.com/downloads/ide/380_ides.exe
if [[ 380_ides.exe -nt ides.exe ]]; then
        cp -f 380_ides.exe ides.exe
        echo "IDEs were updated."
else
        echo "IDEs do not need to be updated."
fi

The -nt stands for newer then.

lyle_s 05-07-2004 03:18 PM

Yet another approach:

Code:

#!/bin/bash

DOWNLOAD_DIR=http://www.sophos.com/downloads/ide
DOWNLOAD_FILE=380_ides.exe
LOCAL_NAME=ides.exe


# Attempt to download the file. --progress=dot:mega is used to prevent
# WGET_OUTPUT from getting too long.
WGET_OUTPUT=$(2>&1 wget --timestamping --progress=dot:mega \
              "$DOWNLOAD_DIR/$DOWNLOAD_FILE")


# Make sure the download went okay.
if [ $? -ne 0 ]
then
        # wget had problems.
        echo 1>&2 $0: "$WGET_OUTPUT"  Exiting.
        exit 1
fi


# Copy the file to the new name if necessary.
if echo "$WGET_OUTPUT" | fgrep 'saved' &> /dev/null
then
        cp -f "$DOWNLOAD_FILE" "$LOCAL_NAME"
        echo "IDEs were updated."
else
        echo "IDEs do not need to be updated."
fi

Lyle

greenhornet 05-09-2004 08:51 PM

Thanks heaps guys, both these solutions work a treat and are now saving my webserver a lot of unessicary work!!

cheers!
GH


All times are GMT -5. The time now is 11:57 PM.