0

我有这个简单的命令来检查文件是否存在:

if [ -f /tmp/file.txt ] ; then echo "yes" ; else echo "no" ; fi

如果我直接在终端上运行它,它可以工作(如果文件存在则显示“是”,如果不存在则显示“否”)。但我想在.desktop文件中执行这个命令,使用它作为Exec键的值:

[Desktop Entry]
Version=1.0
Type=Application
Exec=if [ -f /tmp/file.txt ] ; then echo "yes" ; else echo "no" ; fi
StartupNotify=true
Terminal=false
Categories=Utility;X-XFCE;X-Xfce-Toplevel;
MimeType=x-scheme-handler/custom
Name=Custom Test
Comment=Custom

如果我尝试执行xdg-open custom://我得到custom://: error opening location: The specified location is not supported,但如果我将Exec值更改为echo "yes"并执行xdg-open custom://,它会显示yes在终端上。

我在这里想念什么?

4

2 回答 2

0

尝试将您的 Exec 设置为:

bash -c 'if [ -f /tmp/file.txt ] ; then echo "yes" ; else echo "no" ; fi'

“if”命令是 bash 内置命令,而不是外部命令。

于 2015-08-04T02:26:57.053 回答
0

您正在尝试在不受支持的 .desktop 文件中执行 shell 脚本编码。

“echo yes”起作用的原因是 .desktop 执行参数为“yes”的 echo 命令,这是可以接受的。

.desktop 执行命令以及选项和参数。您可以在 .sh 文件中编写 shell 脚本代码,并在 Exec 或 Run the code using 中提及它

Exec=sh -c "if [ -f /tmp/file.txt ] ; then echo 'yes' ; else echo 'no' ; fi"

这里 .desktop 使用选项和参数执行“sh”

于 2015-08-04T09:21:22.737 回答