Jas*_*ker 431 shell rm makefile
我正在编写一个 makefile,它将在编译结束时清理一些无用的文件。如果已经创建了一个目标,它当然会跳过该目标,而无用的文件可能不存在。所以如果我这样做:
rm lexer.ml interpparse.ml interpparse.mli
Run Code Online (Sandbox Code Playgroud)
我可能会收到错误,因为其中一个文件不存在。有没有办法告诉rm
忽略这些文件?
在阅读手册页时,我看到以下选项:
-f Attempt to remove the files without prompting for confirma-
tion, regardless of the file's permissions. If the file does
not exist, do not display a diagnostic message or modify the
exit status to reflect an error. The -f option overrides any
previous -i options.
Run Code Online (Sandbox Code Playgroud)
这听起来几乎是我想要的,但我不太确定权限部分。有没有办法做到这一点?
Dav*_*ebb 365
该-f
选项绝对是您想要使用的。
它所指的关于文件权限的确认是这样的:
$ touch myfile
$ chmod 400 myfile
$ rm myfile
rm: remove write-protected regular empty file `myfile'?
Run Code Online (Sandbox Code Playgroud)
因此rm
,如果您尝试删除没有写入权限的文件,则会警告您。如果您对该目录具有写权限,则允许这样做,但有点奇怪,这就是rm
通常会警告您的原因。
Gie*_*ers 177
另一种解决方案是:https : //stackoverflow.com/questions/11231937/bash-ignoring-error-for-a-particular-command
只需在您的命令后添加一个 OR 语句:
rm -rf my/dir || true
Run Code Online (Sandbox Code Playgroud)
这样,当语句 #1 失败(抛出错误)时,运行语句 #2,它只是true
.
小智 85
我参加聚会迟到了,但我一直在使用它。在 makefile 中,添加-
到一行的开头以忽略该行的返回值。像这样:
-rm lexer.ml interpparse.ml interpparse.mli
小智 21
如果您不想使用 -f 选项,另一种方法是:
rm filethatdoesntexist 2> /dev/null || true
Run Code Online (Sandbox Code Playgroud)
这只会防止打印错误。
-f 选项意味着如果出现不符合预期的情况,您将不会收到提示。这并不意味着不考虑权限。
如果您没有足够的权限删除文件,则不会删除该文件。
但是,如果您有足够的权限来更改权限,您的文件将被删除。当您是具有所有者只读权限 (-r--------) 的文件的所有者时,就会出现这种情况。作为所有者,您可以chmod u+w
,然后删除它:rm -f
将删除该文件。
小智 6
也许可以帮助类似于以下内容的行:
touch fakefile.exe fakefile.o && rm *.o *.exe
Run Code Online (Sandbox Code Playgroud)
我知道这不是很聪明,但它可以完成工作。
小智 5
首先测试该文件是否存在,如果存在,则将其传递给 rm。那么来自 rm 的错误将是有意义的。-f 或忽略错误消息通常比人们想要的更具包容性。-f 可能会做你不想做的事情。
if [ -f lexer.ml ]; then
rm lexer.ml
fi
Run Code Online (Sandbox Code Playgroud)
如果您想确保存在更多文件,请添加更多测试子句。
归档时间: |
|
查看次数: |
368684 次 |
最近记录: |