【问题标题】:Hashbang for Gnome .desktop filesGnome .desktop 文件的 Hashbang
【发布时间】:2011-08-26 13:55:48
【问题描述】:

我希望能够在我的.desktop 文件的顶部添加一个#! 注释,这样如果它具有执行权限并被执行,它就会真正运行。但是,我不知道.desktop 文件的解释器是什么,所以我不知道在hashbang 中写入哪个/usr/bin/ 文件。有什么想法吗?


编辑:

到目前为止,我已经制作了一个小的 bash 脚本,execdesktop,它可以执行桌面文件:

`sed -nr 's/Exec=(.*)$/\\1/p' $1`

如果我随后将以下内容添加到我的 .desktop 文件中:

#!/usr/bin/execdesktop

然后它运行良好。此方法有效,但我不想使用它,因为它需要安装 execdesktop。

【问题讨论】:

  • 除非您使用系统包管理器(dpkg?RPM?emerge?等)创建了一个安装execdesktop 的包,否则脚本可能应该在/usr/local/bin 中,而不是@ 987654333@.

标签: linux bash scripting gnome


【解决方案1】:

没有; .desktop 文件不打算被执行。改为运行Exec 键中给出的可执行文件。

【讨论】:

  • 它们以一种或另一种方式执行。绝对没有办法从命令行调用 .desktop 文件?这似乎不太像 linux...
  • 桌面文件不会被执行。它们内部具有可执行命令行的行是被执行的行。文件的其余部分由桌面环境解析。
  • 问题是,在我的例子中,Exec 属性中提到的脚本有效,但桌面文件仅在 Ubuntu 下无效(它在 Mageia 上有效)。这就是为什么在命令行中打开桌面文件以便它以与操作系统相同的方式执行命令的原因。
【解决方案2】:

您始终可以将xdg-open 用于您的shebang,如下所示:

#!/usr/bin/env xdg-open

这不会造成任何麻烦,因为# 也在.desktop 文件中启动 cmets。

【讨论】:

  • 有趣,虽然 xdg-open 似乎想在 gedit 中打开我所有的 .desktop 文件,而不是实际启动它们。
  • @Reinderien:您是否将.desktop 文件关联到geditxdg-mime query default application/x-desktop 返回什么?
  • @Reinderien:不管怎样,你应该可以使用#!/bin/sed -ne s/^Exec=//e作为你的shebang线。
  • @ninjalj xdg-open 问题(在 gedit 中打开)是由于 gnome 中的错误。仍然有一个解决方法。请在 [askubuntu] (askubuntu.com/questions/5172/…) 上查看我对同一问题的回答
  • 这个答案是错误的。桌面文件不打算永远执行。在某些机器上 xdg-open MIGHT 工作的事实只是文件关联的问题,但它仍然是错误的。不要运行 .desktop 文件,不要让它们可执行。
【解决方案3】:

明确地说,Ignacio 是正确的here,因为不应直接执行 .desktop 文件。这是可能的(正如您所发现的),但不明智。

另外注意,不要使用xdg-open。如果有正确关联的 mime 类型,它可能会碰巧起作用,但这并不可靠。

您应该使用gtk-launch。用法如下:

gtk-launch APPLICATION [URI...]
gtk-launch app-name.desktop
gtk-launch app-name

这里是 man 条目:

名字

   gtk-launch - Launch an application

概要

   gtk-launch [APPLICATION] [URI...]

描述

   gtk-launch launches an application using the given name. The
   application is started with proper startup notification on a default
   display, unless specified otherwise.

   gtk-launch takes at least one argument, the name of the application to
   launch. The name should match application desktop file name, as
   residing in /usr/share/application, with or without the '.desktop'
   suffix.

   If called with more than one argument, the rest of them besides the
   application name are considered URI locations and are passed as
   arguments to the launched application.

请注意gtk-launch 需要安装.desktop 文件(即位于/usr/share/applications$HOME/.local /share/applications)。

所以为了解决这个问题,我们可以使用一个 hackish 的小 bash 函数,在启动它之前临时安装所需的 .desktop 文件。安装 .desktop 文件的“正确”方法是通过desktop-file-install,但我将忽略它。

launch(){
    (
    # where you want to install the launcher to
    appdir=$HOME/.local/share/applications

    # the template used to install the launcher
    template=launcher-XXXXXX.desktop

    # ensure $1 has a .desktop extension, exists, is a normal file, is readable, has nonzero size
    # optionally use desktop-file-validate for stricter checking
    # if ! desktop-file-validate "$1" 2>/dev/null; then
    if [[ ! ( $1 = *.desktop && -f $1 && -r $1 && -s $1 ) ]]; then
        echo "ERROR: you have not supplied valid .desktop file" >&2
        exit 1
    fi

    # ensure the temporary launcher is deleted upon exit
    trap 'rm "$launcherfile" 2>/dev/null' EXIT

    launcherfile=$(mktemp -p "$appdir" "$template")
    launchername=${launcherfile##*/}

    if cp "$1" "$launcherfile" 2>/dev/null; then
        gtk-launch "$launchername" "${@:2}"
    else
        echo "ERROR: failed to copy launcher to applications directory" >&2
        exit 1
    fi

    exit 0
    )
}

您可以像这样使用它(如果需要,还可以传递其他参数或 URI):

launch ./path/to/shortcut.desktop

另外,我写了一个答案here,概述了启动 .desktop 文件的所有方法。它提供了gtk-launch 的一些替代方案,可能会有所帮助。

【讨论】:

  • 对技术优点表示赞同;但应该接受的正确答案是 Ignacio Vazquez-Abrams 的答案。
猜你喜欢
  • 2020-05-21
  • 2020-06-13
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 2022-01-17
  • 2014-05-14
  • 1970-01-01
相关资源
最近更新 更多