观察 .tar 文件是否有更改,如果有更改,则提取、构建并运行

发布于 2025-01-04 10:44:06 字数 470 浏览 1 评论 0原文

我正在尝试观察 tar 文件的更改,如果有更改,则提取文件,将其 cd 到目录中,然后构建,然后运行。

我的情况如下:

while [ 1 ]
do
    checksum=`md5sum kinect.tar`
    sleep 10
    newsum=`md5sum kinect.tar`
    if [ $newsum -ne $checksum ]
    then
        killall lebron
        tar -zxf kinect.tar
        cd kinect
        make
        ./lebron &
    fi
done

有时,我会在 if 行上收到 [ has too much argument 错误,但我不确定为什么。我的东西还好吗?您会建议替代解决方案吗?

I am trying to watch a tar file for changes, and if it does, extract the files, cd into the dir, and build, and then run.

What I have is as follows:

while [ 1 ]
do
    checksum=`md5sum kinect.tar`
    sleep 10
    newsum=`md5sum kinect.tar`
    if [ $newsum -ne $checksum ]
    then
        killall lebron
        tar -zxf kinect.tar
        cd kinect
        make
        ./lebron &
    fi
done

Occasionally I get a [ has too many arguments error on the line with the if, but I'm not sure why. Is what I have OK? Would you suggest an alternative solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2025-01-11 10:44:06

您的问题之一是 md5sum 返回哈希文件名。您只想捕获哈希值。您可以使用带有重定向和进程替换的内置read轻松完成此操作。

另外,由于您已经使用 bash 作为解释器,因此您确实应该使用 [[ ]] 而不是 [ ] ,前者是更有能力。

#!/bin/bash

while true; do
    read checksum _ < <(md5sum kinect.tar)
    sleep 10
    read newsum _ < <(md5sum kinect.tar)
    if [[ $newsum != $checksum ]]; then
        killall lebron
        tar -zxf kinect.tar
        cd kinect
        make
        ./lebron &
    fi
done

One of your problems is that md5sum returns both the hash and the file name. You just want to capture the hash. You can easily do this using the read builtin with redirection and process substitution.

Also, since you're already using bash as your interpreter you really aught to use [[ ]] over [ ] and the former is a lot more capable.

#!/bin/bash

while true; do
    read checksum _ < <(md5sum kinect.tar)
    sleep 10
    read newsum _ < <(md5sum kinect.tar)
    if [[ $newsum != $checksum ]]; then
        killall lebron
        tar -zxf kinect.tar
        cd kinect
        make
        ./lebron &
    fi
done
安人多梦 2025-01-11 10:44:06

首先,您不引用变量。其次,-ne 用于比较数字,而不是字符串。使用 != 代替:

if [ "$newsum" != "$checksum" ]

正如 Ed Heal 在他的评论中指出的那样,该算法只有在文件被原子修改时才可能有意义。保证这一点的最佳方法是在一个地方生成它,然后移动到 kinect.tar。另外,如果在构建以前的版本时修改了文件,您的代码将不会捕获更改,这可以通过 Ed Heal 建议的以下更改来修复:

checksum=`md5sum kinect.tar`
while [ 1 ]
do
    sleep 10
    old_checksum=$checksum
    checksum=`md5sum kinect.tar`
    if [ "$old_checksum" != "$checksum" ]

真的,如果您有更好的方法,那就最好了编写 .tar 文件以与您的脚本进行通信的过程。例如,新的 .tar 文件始终完全写入,然后移动到 kinect-new.tar,脚本会检查该文件并移动到 kinect.tar< /代码>。这既避免了读取不完整文件的可能性,也避免了比较 md5sum 的混乱。 (您可以更喜欢使用 inotify-tools 之类的东西来避免显式轮询,但可能不值得付出努力。)

不幸的是,这并不能解决构建可能失败的问题(除非您使用的 .tar 文件是已知的良好版本,在这种情况下似乎不太可能你想要检查的每 10 秒更新一个)。您可能想要创建一个目录,在那里解压,并且仅在 make 成功时才替换旧版本。类似这样的事情:

tar -zxf kinect.tar
cd kinect
if make
then
    cd ..
    rm -rf kinect.good
    mv kinect kinect.good
    cd kinect.good
    killall lebron
    ./lebron &
fi
cd .. # make sure we always stay in the original directory

First, you don't quote your variables. Second, -ne is for comparing numbers, not strings. Use != instead:

if [ "$newsum" != "$checksum" ]

As Ed Heal pointed out in his comment, the algorithm only possibly makes sense if the file is being modified atomically. The best way to guarantee that is if it is generated in one place and then moved to kinect.tar. Also, if the file is modified while the previous version is being built, your code will not catch the change, which can be fixed by the following change suggested by Ed Heal:

checksum=`md5sum kinect.tar`
while [ 1 ]
do
    sleep 10
    old_checksum=$checksum
    checksum=`md5sum kinect.tar`
    if [ "$old_checksum" != "$checksum" ]

Really, it would be best if you had a better way for the process writing the .tar file to communicate with your script. For example, the new .tar files are always fully written and then moved to kinect-new.tar which the script checks for and moves to kinect.tar. That avoids both the possibility of reading an incomplete file and the messiness of comparing md5sums. (You could be fancier and use something like inotify-tools to avoid having to explicitly poll, but it's possibly not worth the effort.)

Unfortunately, that does not solve the problem that the build may fail (unless the .tar files you are using are known good releases, in which case it seems unlikely that you would want to check for a new one every 10 seconds). You probably want to make a directory, untar there, and only replace the old version if make succeeds. Something along the lines of this:

tar -zxf kinect.tar
cd kinect
if make
then
    cd ..
    rm -rf kinect.good
    mv kinect kinect.good
    cd kinect.good
    killall lebron
    ./lebron &
fi
cd .. # make sure we always stay in the original directory
站稳脚跟 2025-01-11 10:44:06

这是对上面 @tekknolagi 评论的回复 - 使用它可以使响应更具可读性。

检查更新的脚本

checksum=`md5sum kinect.tar` 

    while [ 1 ] 

do 
    sleep 10 
    newsum=`md5sum kinect.tar` 
    if [ "$newsum" != "$checksum" ] 
    then 
       # Inform you that the file has changed (email perhaps).
    fi 

我假设您通过网络(FTP?)接收 tar 文件。如果是这样,请执行以下操作

1) FTP 到不同的文件名
2) FTP 小尺寸(1 字节)表示 FTP 已完成
3) 修改上述脚本以检查步骤 2 收到的文件。如果找到,请将其删除(或者更好地重命名它以便于轻松恢复),删除旧的 kinect.tar 文件。将步骤 1 中的文件移动到位

1) FTP 到不同的文件名
2)获取FTP删除kinect.tar
3) 使用 FTP 将文件从 1) 移动到位。

This is a reply to @tekknolagi comment above - using this to make the response more readable.

Script to check for an update

checksum=`md5sum kinect.tar` 

    while [ 1 ] 

do 
    sleep 10 
    newsum=`md5sum kinect.tar` 
    if [ "$newsum" != "$checksum" ] 
    then 
       # Inform you that the file has changed (email perhaps).
    fi 

I am assuming that you are receiving the tar file via a network (FTP?). If so do the following

1) FTP to a different file name
2) FTP a small size (1 byte) to indicate FTP is complete
3) Modify the above script to check for the file received by step 2. If found, delete it (or better still rename it to enable you to easily recover), remove the old kinect.tar file. move the file from step 1 into place

Or

1) FTP to a different file name
2) Get FTP to delete kinect.tar
3) Get FTP to move file from 1) into place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文