检查文件或文件夹是否已被修补

dai*_*isy 27 patch

在应用补丁之前,是否有可能知道文件是否已经打过补丁?

我需要在脚本中执行此操作,有什么想法吗?

poi*_*ige 26

是的,只需patch使用--dry-run选项运行,它要么失败,要么成功,这可以通过退出状态找到。

但在更一般(且不易出错)的方式中,您可能必须使用-R选项运行它,这意味着“反向”,因为只有当它能够还原整个补丁时,它才可以被视为“已应用”。否则(没有'-R')它可能会因为原始文件的某些部分被更改而失败。下面是一个简单的例子:

if ! patch -R -p0 -s -f --dry-run <patchfile; then
  patch -p0 <patchfile
fi
Run Code Online (Sandbox Code Playgroud)

(甚至,在上面的代码段中,您甚至可能更喜欢patch将其 stdout 和 stderr 完全重定向到静音/dev/null

  • 顺便说一句,如果你使用 Git,同样的想法也可以,但使用 `git apply -R --check` 代替。 (3认同)

Div*_*s01 17

以防万一它对某人有帮助,如果您使用的是 bash 脚本,那么 Omnifarious 给出的示例将不起作用。在 bash 中,成功命令的退出状态为 0

因此,以下将起作用:

patch -p0 -N --dry-run --silent < patchfile 2>/dev/null
#If the patch has not been applied then the $? which is the exit status 
#for last command would have a success status code = 0
if [ $? -eq 0 ];
then
    #apply the patch
    patch -p0 -N < patchfile
fi
Run Code Online (Sandbox Code Playgroud)

  • 不,0 是正确的。如果出于某种原因试运行失败,补丁将以非零值退出,在这种情况下不应应用补丁。 (2认同)

Omn*_*ous 2

这是一个猜测,假设您正在使用该patch实用程序并且每个要修补的文件都有自己的补丁:

if patch <options> -N --dry-run --silent <patchfile 2>/dev/null; then
    echo The file has not had the patch applied,
    echo and the patch will apply cleanly.
else
    echo The file may not have had the patch applied.
    echo Or maybe the patch doesn't apply to the file.
fi
Run Code Online (Sandbox Code Playgroud)

  • 您能否详细说明一下为什么在“if”情况下选择使用“nohup”? (8认同)