如何只做一次

如何只做一次

我正在编写一个脚本,用于下载一些文件并执行它们。我只希望每个文件名由下载它们的主机执行一次。我正在尝试创建一个脚本,该脚本可以提取文件,如果尚未执行则执行它们,然后跟踪已执行的每个文件以防止重复运行。到目前为止,我似乎只能每次执行所有内容,并且备份记录不会触发重复项的删除。

(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

我究竟做错了什么?

答案1

bash $eachmv $each $each.bak放在一个else块中:

(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

这样,仅当/usr/bin/test -e "$each.bak"失败时才会执行和移动当前脚本(即,当前脚本之前尚未被执行和移动)。

我将所有变量都用双引号引起来,以防止它们破坏命令,以防它们最终包含奇怪的字符/字符串。

相关内容