如何测试 rm 的 GNU 或 BSD 版本?
GNU 版本的 rm
有一个很酷的 -I 标志。从联机帮助页来看:
-I prompt once before removing more than three files, or when removing recursively. Less
intrusive than -i, while still giving protection against most mistakes
Mac 没有:
$ rm -I scratch
rm: illegal option -- I
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
有时人们在 Mac 上安装了 coreutils(GNU 版本),有时却没有。有没有办法在继续之前检测此命令行标志?我想在我的 bash_profile 中有这样的内容:
if [ has_gnu_rm_version ]; then
alias rm="rm -I"
fi
The GNU version of rm
has a cool -I flag. From the manpage:
-I prompt once before removing more than three files, or when removing recursively. Less
intrusive than -i, while still giving protection against most mistakes
Macs don't:
$ rm -I scratch
rm: illegal option -- I
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
Sometimes people have coreutils
(the GNU version) installed on Macs and sometimes they don't. Is there a way to detect this command line flag before proceeding? I'd like to have something like this in my bash_profile:
if [ has_gnu_rm_version ]; then
alias rm="rm -I"
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
字符串 /bin/rm | grep -q 'GNU coreutils'
如果 $?是0,是coreutils
strings /bin/rm | grep -q 'GNU coreutils'
if $? is 0, it is coreutils
我建议根本不要走这条路。将您的脚本定位为尽可能可移植,并且仅依赖于您可以信赖的标志/选项/行为。 Shell 脚本编写已经够难的了——为什么要增加更多的出错空间呢?
您可以查看POSIX 官方网站了解实用程序命令的规范。例如,rm。
I would recommend not starting down this road at all. Target your scripts to be as portable as possible, and only rely on flags/options/behaviors you can count on. Shell scripting is hard enough - why add more room for error?
You can check the official POSIX website for their specifications of utility commands. For example, rm.
我想说在临时文件上测试 rm -I 的输出,如果通过则使用别名
I'd say test the output of
rm -I
on a temp file, if it passes then use the alias您始终可以使用
--version
询问 rm 其版本,并检查它是否显示 gnu 或 coreutils,如下所示:You could always ask rm its version with
--version
and check to see if it says gnu or coreutils like this:像这样的事情怎么样?
how about something like this?