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.
没有一个; .desktop文件无意执行.Exec改为运行密钥中给出的可执行文件.
您可以随时将其xdg-open用于shebang,如下所示:
#!/usr/bin/env xdg-open
Run Code Online (Sandbox Code Playgroud)
这不会造成任何麻烦,因为#也会在.desktop文件中开始注释。
只要是明确的,伊格纳西奥是正确的位置在的.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)
这是男人的入口:
名称
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.
请注意,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可能有用的替代方案.