I assume your expectations are wrong. The .desktop files are doing where they were designed for: starting a program from a desktop environment by clicking on an icon. They are
not for storing programs inside them as argument to the Exec = entry.
Same as on a command line interface, one starts a program by typing the file where the executable is and not by typing the complete program.
So how would I try to tackle this.
When one wants to start a program from a desktop by clicking on an icon, first there must be a program. Programs come basically in two kinds:
- Compiled programs: programs written in languages like Cobol, Fortran, C, C++, that are compiled into code and linked to create an executable file;
- Interpreted programs: programs written in languages like BASIC, Python, Ruby and shells like sh, ksh, csh and (at last) bash, they are text only and interpreters read that files, convert it into code on the spot and execute that code. That is of course less efficient then compiled programs when executed more often because the translating into code is done every time again.
In your case you have a bash script (the word used for programs of shells). Why do I mention all the other types of executables? Because I hope it will be clear to you now that trying to put the whole program in the Exec = entry can at least not be done for compiled programs and will be very difficult to do for a interpreted one (who should interpret it?)
OK, thus the user has (or creates) a very useful bash script and stores that in a suitable place. What is a suitable place? When that script is part of a whole lot of scripts/data/etc. the user might want to use a special directory within his home directory. When the user is not sure, there is the bin directory inside the home directory, created at user creation just for that. Assuming the bash script is called nicescript and the user is jcdole, that would then be in /home/jcdole/bin/nicescript. Do not forget to set at least the execute permission for the owner
Code:
chmod u+x /home/jcdole/bin/nicescript
And I repeat that that bash script is only a bash script when it has a correct "shebang", that is in the
first line:
Your program is ready now. Next step is the .desktop file. I will only talk about the Exec = entry:
Code:
Exec = /user/jcdole/bin/nicescript
It is always good to use absolute path in such cases. You can of course add some arguments at the end when you need them.
It is that easy. And now you can even change the script, by just editing the script. The desktop icon will always start the up-to-date script.
You expressed your wish to let a .desktop file start a program with root as owner.
For this I took a look at how YaST is stated in my KDE:
Code:
/usr/bin/xdg-su -c /sbin/yast2
So I assume that something like
Code:
/usr/bin/xdg-su -c /usr/local/bin/importantscript
will work (I did not test this, I am very careful in using root).
But please, remark that I use /usr/local/bin as the place to store the program. You should never execute as root a program that is not fully under control of root. Specially when it is a script. Changing by the user is oh so easy and then root runs something rotten. So only run as root programs that are in in the system part, protected from access by ordinary users (I took /usr/local/bin as example, but large products might e.g. go into /opt)
A bottom line: do use features only for what they were designed for. Else you will follow a marshy and misty path when you try to achieve your goal and in most cases drown somewhere halfway.