“xdg-mime 查询文件类型”返回与“file --mime-type”不同的文件类型

Val*_* B. 5 shell mime-types

我有一个运行来创建电子邮件的 bash 脚本。我现在不想修改它,因为它在我当前的项目中非常重要。一个命令在操作机器和我的机器上运行的不同:

xdg-mime query filetype <file>
Run Code Online (Sandbox Code Playgroud)

它在一个简单的us-ascii编码文本文件(带有客户文件扩展名)上运行。问题是,在脚本工作的操作机器上,它返回plain/text(预期行为)。xdg-mime 的调试模式显示它实际上是file -i在运行的机器上运行命令。但是在我的机器上,它返回application/octet-stream并运行一个gnomevfs-info命令。它似乎与桌面环境有关(两台机器都在 gnome 上运行)。

有没有办法强制xdg-mime运行file -i?或者让gnomevfs-info返回正确的 mime 类型?我尝试取消设置,GNOME_DESKTOP_SESSION_ID但这是做什么的xdg-mime

detectDE()
{
    if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
    elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
    elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
    fi
}
Run Code Online (Sandbox Code Playgroud)

第三个elif是导致xdg-mime使用gnomevfs-infoover file -i,因为该命令返回 0 并且 DE 设置为 gnome。我试过查看手册页,但该命令dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1对我来说是胡言乱语。

zep*_*lin 3

如果您提前知道自定义文本文件扩展名,则可以简单地为它们注册一个新的 MIME 条目,作为解决方法。

例如,如果您的文件的扩展名为“.list”:

准备描述符文件:zeppelin-list.xml

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="text/plain">
    <comment>List file type</comment>
    <glob pattern="*.list"/>
  </mime-type>
</mime-info>
Run Code Online (Sandbox Code Playgroud)

注册一个新的 MIME 条目:

%xdg-mime install zeppelin-list.xml
Run Code Online (Sandbox Code Playgroud)

询问:

%xdg-mime query filetype my.list
text/plain
Run Code Online (Sandbox Code Playgroud)