Hashbang为Gnome .desktop文件

Rei*_*ien 7 linux bash scripting gnome

我希望能够#!在我的.desktop文件顶部添加注释,以便如果它具有执行权限并执行,它将实际运行.但是,我不知道.desktop文件的解释器是什么,所以我不知道/usr/bin/在hashbang中写入哪个文件.有任何想法吗?


编辑:

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

`sed -nr 's/Exec=(.*)$/\\1/p' $1`
Run Code Online (Sandbox Code Playgroud)

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

#!/usr/bin/execdesktop
Run Code Online (Sandbox Code Playgroud)

然后运行正常.这种方法有效,但我不想使用它,因为它需要安装execdesktop.

Ign*_*ams 5

没有一个; .desktop文件无意执行.Exec改为运行密钥中给出的可执行文件.

  • 他们以这样或那样的方式被处决。绝对没有办法从命令行调用 .desktop 文件吗?这看起来不太像linux... (2认同)
  • 桌面文件不会被执行.其中包含可执行命令行的行是执行的行.桌面环境会解析文件的其余部分. (2认同)

nin*_*alj 5

您可以随时将其xdg-open用于shebang,如下所示:

#!/usr/bin/env xdg-open
Run Code Online (Sandbox Code Playgroud)

这不会造成任何麻烦,因为#也会在.desktop文件中开始注释。

  • 这个答案是错误的。桌面文件不打算永远执行。在某些机器上,xdg-open MIGHT可以正常运行的事实仅仅是文件关联的问题,但这仍然是错误的。不要运行.desktop文件,不要使它们可执行。 (7认同)
  • @Reinderien:无论如何,您应该可以将`#!/ bin / sed -ne s / ^ Exec = // e`用作shebang行。 (4认同)
  • @ninjalj xdg-open问题(在gedit中打开)是由于gnome中的错误所致。仍然有一种解决方法。请在[askubuntu](http://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal)中查看我对相同问题的回答 (3认同)

Six*_*Six 5

只要是明确的,伊格纳西奥是正确的位置的.desktop文件不应该直接执行.有可能(如你所发现的那样),但不明智.

另外请注意,不要使用xdg-open.如果存在正确关联的mime类型,它可能恰好可能正常工作,但这不可靠.

你应该用gtk-launch.它使用如下:

gtk-launch APPLICATION [URI...]
gtk-launch app-name.desktop
gtk-launch app-name
Run Code Online (Sandbox Code Playgroud)

这是男人的入口:

名称

   gtk-launch - Launch an application
Run Code Online (Sandbox Code Playgroud)

概要

   gtk-launch [APPLICATION] [URI...]
Run Code Online (Sandbox Code Playgroud)

描述

   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.
Run Code Online (Sandbox Code Playgroud)

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

因此,为了解决这个问题,我们可以使用一个hackish little 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
    )
}
Run Code Online (Sandbox Code Playgroud)

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

launch ./path/to/shortcut.desktop
Run Code Online (Sandbox Code Playgroud)

或者,我在这里写了一个答案,概述了启动.desktop文件的所有方法.它提供了一些gtk-launch可能有用的替代方案.