bash 如何检查 RPM .spec 文件中是否存在文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25311416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:07:59  来源:igfitidea点击:

How to check if a file exists in an RPM .spec file?

linuxbashrpmrpm-spec

提问by John

I have a spec file that needs to check if an older version of a file exists and if so delete it. For some reason, it is running the delete command regardless of whether the file exists and printing a "no such file or directory" error, which leads me to believe my if statement checking if the file exists is at fault. Here is my code.

我有一个规范文件,需要检查文件的旧版本是否存在,如果存在则将其删除。出于某种原因,无论文件是否存在,它都会运行删除命令并打印“没有这样的文件或目录”错误,这让我相信我的 if 语句检查文件是否存在是有问题的。这是我的代码。

if [ -e "/foo/bar/file.zip" ]; then
    rm -rf /foo/bar/file.zip
fi

Any help is appreciated.

任何帮助表示赞赏。

回答by Etan Reisner

That rm -rfcommand cannotbe the source of that error message because -ftells rmto never fail (and to never print such error messages, try it locally rm -f /this/is/some/path/that/does/not/exist; echo $?). (This means, of course, that the test for file existence itself is unnecessary since rm -fdoes not care.

rm -rf命令不能成为该错误消息的来源,因为它-f告诉rm永远不要失败(并且永远不要打印此类错误消息,请在本地尝试rm -f /this/is/some/path/that/does/not/exist; echo $?)。(当然,这意味着文件存在本身的测试是不必要的,因为rm -f它不在乎。

Additionally you do not need the -rflag if you are deleting a file (and it is safer not to include it (or -f) when you do not need them.

此外,-r如果您要删除文件,则不需要该标志(并且-f在不需要它们时不包含它(或)更安全。

So something else must be printing that message. Do you use that file anywhere else? Does anything from the old package's %preunuse it perhaps?

因此,必须有其他东西在打印该消息。你在其他地方使用那个文件吗?旧包中的任何东西都可以%preun使用它吗?

回答by Darshan Patel

[ -f /foo/bar/file.zip ] && rm -f /foo/bar/file.zip || echo "File Not Found"

回答by konsolebox

It's possible that you didn't add a space between the file path and -e:

您可能没有在文件路径和 之间添加空格-e

if [ -e"/foo/bar/file.zip" ]; then
    rm -rf /foo/bar/file.zip
fi

It would be synonymous to this which is always true:

这将是这个永远正确的同义词:

if [ -n "-e/foo/bar/file.zip" ]; then
    rm -rf /foo/bar/file.zip
fi