【问题标题】:Implement a kind of infinite while loop实现一种无限的while循环
【发布时间】:2011-05-04 07:37:05
【问题描述】:

目前,我的脚本下载html页面的源代码并将其保存为plist,它检查plist和模板文件的SHA哈希,如果哈希不同,它会删除plist中的一些东西,否则它退出了。

我想要实现的是一种无限的while循环。虽然 SHA 哈希值相同,但它会再次下载 html 源代码,检查 SHA 哈希值,当检测到 SHA 哈希值不同时,它会删除 plist 中的一些键。

 #!/bin/sh

 file="/a/path/file"

 a="Key1"
 b="Key2"
 c="Key3"
 d="Key4"

 declare -a array=($a $b $c $d);

 cd /a/path
 if [ ! -e $file.plist ]; then
 curl http://something.com/ > file.plist
 fi

 new=`shasum file.plist`
 old=`shasum orig_file.plist` # this is a template file.


 if [ "$old" != "$new" ]; then

 echo "Hash mismatch !"

 for i in "${array[@]}"
 do
defaults delete $file $i
 done
 else
 exit 0
 fi

【问题讨论】:

    标签: macos bash curl while-loop


    【解决方案1】:

    您真的应该使用 cron 条目来做到这一点。无限循环是邪恶的。

    【讨论】:

    • Cron 需要额外的工作来处理锁定文件以避免重复的同时运行。
    • 是的,这没什么大不了的。
    【解决方案2】:

    这是一个无限循环:

    while sleep 1
    do
      perform_check
    done
    

    【讨论】:

      【解决方案3】:

      如果遥控器较新,则只下载文件的新版本是有意义的:

      $ curl http://something.com/ -o file.plist -z file.plist

      【讨论】:

        猜你喜欢
        • 2015-06-25
        • 2014-03-29
        • 2012-12-24
        • 2013-02-22
        • 2011-11-19
        • 1970-01-01
        • 2016-08-27
        • 2011-09-26
        • 2015-04-29
        相关资源
        最近更新 更多