1

I am writing a script that downloads some files and executes them. I only want each filename executed once by the hosts that are downloading them. I am trying to create a script that pulls the files, executes them if they havent already been executed, and then keeps track of each file that has been executed to prevent duplicate runs. So far all I seem to be able to do is execute all each time, and the backup record does not trigger the deletion of the duplicates.

(cd /opt/trunk/mythos/medusa/remote-scripts/ && wget -r --no-parent --reject "index.html*" http://server/medusa/scripts/)
chmod +x /opt/trunk/mythos/medusa/remote-scripts/server/medusa/scripts/*.sh
for each in /opt/trunk/mythos/medusa/remote-scripts/server/medusa/scripts/*.sh
do
    if /usr/bin/test -e $each.bak
    then
        rm -rf /opt/trunk/mythos/medusa/remote-scripts/server/medusa/scripts/$each
    fi
    bash $each
    mv $each $each.bak
done

What am I doing wrong?

3
  • I think I got the problem, but I don't get the difference between wget -H -r http://server/medusa/scripts/* and wget -r --no-parent --reject "index.html*" http://server/medusa/scripts/. Mind to explain it?
    – kos
    Nov 19, 2015 at 20:44
  • the first one was commented out. copy+paste error. I removed it.
    – spyderdyne
    Nov 19, 2015 at 20:59
  • Using aria2 instead of wget should solve this issue as aria2c does this; it keeps track of what has been downloaded in the current directory and will not download any download that has completed.
    – mchid
    Nov 20, 2015 at 3:33

1 Answer 1

2

Put bash $each and mv $each $each.bak in an else block:

(cd /opt/trunk/mythos/medusa/remote-scripts/ && wget -r --no-parent --reject "index.html*" http://server/medusa/scripts/)
chmod +x /opt/trunk/mythos/medusa/remote-scripts/server/medusa/scripts/*.sh
for each in /opt/trunk/mythos/medusa/remote-scripts/server/medusa/scripts/*.sh
do
    if /usr/bin/test -e "$each.bak"
    then
        rm -rf "/opt/trunk/mythos/medusa/remote-scripts/server/medusa/scripts/$each"
    else
        bash "$each"
        mv "$each" "$each.bak"
    fi
done

That way the current script will be executed and moved only if /usr/bin/test -e "$each.bak" fails (i.e. the current script hasn't been executed and moved previously).

I quoted all the variables with double quotes to prevent them from breaking the commands in case they end up containing weird characters / strings.

2
  • 1
    You should quote the variables =) . The comment is nice but it's your answer. ;)
    – A.B.
    Nov 21, 2015 at 17:22
  • @A.B. Fair enough. Happy now? ;)
    – kos
    Nov 21, 2015 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .