[英]How to check if a file exists in an RPM .spec file?
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. 由于某种原因,无论文件是否存在,它都会运行delete命令,并显示“没有这样的文件或目录”错误,这使我相信我的if语句检查文件是否存在错误。 Here is my code. 这是我的代码。
if [ -e "/foo/bar/file.zip" ]; then
rm -rf /foo/bar/file.zip
fi
Any help is appreciated. 任何帮助表示赞赏。
That rm -rf
command cannot be the source of that error message because -f
tells rm
to never fail (and to never print such error messages, try it locally rm -f /this/is/some/path/that/does/not/exist; echo $?
). 该rm -rf
命令不能成为该错误消息的来源,因为-f
告诉rm
永不失败(并且从不打印此类错误消息,请在本地尝试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 -f
does not care. (当然,这意味着不需要测试文件本身是否存在,因为rm -f
无关紧要。
Additionally you do not need the -r
flag 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 %preun
use it perhaps? 旧包的%preun
任何内容是否%preun
使用?
[ -f /foo/bar/file.zip ] && rm -f /foo/bar/file.zip || echo "File Not Found"
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.