如何测试 rm 的 GNU 或 BSD 版本?

发布于 2024-11-26 14:15:50 字数 568 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮光沉寂 2024-12-03 14:15:50

字符串 /bin/rm | grep -q 'GNU coreutils'

如果 $?是0,是coreutils

strings /bin/rm | grep -q 'GNU coreutils'

if $? is 0, it is coreutils

╭⌒浅淡时光〆 2024-12-03 14:15:50

我建议根本不要走这条路。将您的脚本定位为尽可能可移植,并且仅依赖于您可以信赖的标志/选项/行为。 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.

冰雪之触 2024-12-03 14:15:50

我想说在临时文件上测试 rm -I 的输出,如果通过则使用别名

touch /tmp/my_core_util_check

if rm -I /tmp/my_core_util_check > /dev/null 2>&1 ; then
    alias rm="rm -I"
else
    rm /tmp/my_core_util_check;
fi

I'd say test the output of rm -I on a temp file, if it passes then use the alias

touch /tmp/my_core_util_check

if rm -I /tmp/my_core_util_check > /dev/null 2>&1 ; then
    alias rm="rm -I"
else
    rm /tmp/my_core_util_check;
fi
与之呼应 2024-12-03 14:15:50

您始终可以使用 --version 询问 rm 其版本,并检查它是否显示 gnucoreutils,如下所示:

rm --version 2>&1 | grep -i gnu &> /dev/null
[ $? -eq 0 ] && alias rm="rm -I"

You could always ask rm its version with --version and check to see if it says gnu or coreutils like this:

rm --version 2>&1 | grep -i gnu &> /dev/null
[ $? -eq 0 ] && alias rm="rm -I"
薆情海 2024-12-03 14:15:50

像这样的事情怎么样?

#!/bin/bash
rm -I &> /dev/null
if [ "$?" == "0" ]; then
    echo coreutils detected
else
    echo bsd version detected
fi

how about something like this?

#!/bin/bash
rm -I &> /dev/null
if [ "$?" == "0" ]; then
    echo coreutils detected
else
    echo bsd version detected
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文