kde - syntax parser/converter for the exec key of .desktop file

Hello.
Is there any parser/converter for converting multi-line stanza to single line that the exec key will accept.

Just an example to clarify my question :

**1- Script 1 **run without any error when copied and executed in KDE konsole.

IP_ADDR="192.168.130.239"
while : ; do
    ping -c2 "$IP_ADDR"
    if  $? -eq 0 ] ; then
        kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive"
        break
    else
        kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?"
         $? -eq 1 ] && break
        sleep 2
    fi
done

see : http://paste.opensuse.org/14691272

**2 - Script 2 **Putting this in a desktop file does not work :

[Desktop Entry]
Comment[en_US]=test script bad v1
Comment=test script bad v1
Exec=IP_ADDR="192.168.130.230"
while : ; do
    ping -c2 "$IP_ADDR"
    if  $? -eq 0 ] ; then
        kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive"
        break
    else
        kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?"
         $? -eq 1 ] && break
        sleep 2
    fi
done
GenericName[en_US]=test script bad v1
GenericName=test script bad v1
Icon=system-run
MimeType=
Name[en_US]=test script bad v1
Name=test script bad v1
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=

see : https://paste.opensuse.org/49048773

**3 - Script 3 **Putting ‘’ at the end of each line does not work :

[Desktop Entry]
Comment[en_US]=test script bad v2
Comment=test script bad v2
Exec=IP_ADDR="192.168.130.230" \
while : ; do \
    ping -c2 "$IP_ADDR" \
    if  $? -eq 0 ] ; then \
        kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive" \
        break \
    else \
         kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No  connection

Are you sure you want to continue pinging ?" \
         $? -eq 1 ] && break \
        sleep 2 \
    fi \
done
GenericName[en_US]=test script bad v1
GenericName=test script bad v1
Icon=system-run
MimeType=
Name[en_US]=test script bad v1
Name=test script bad v1
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=

see : https://paste.opensuse.org/46049575

**4 - Script 4 **Finaly after different tests i found some solution which work :

[Desktop Entry]
Comment[en_US]=test script good v1
Comment=test script good v1
Exec=IP_ADDR="192.168.130.239" ; while : ; do  ping -c2 "$IP_ADDR" ; if  $? -eq 0 ] ; then  kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive" ; break ; else  kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?" ;   $? -eq 1 ] && break ; sleep 2 ;  fi  ;  done
GenericName[en_US]=test script good v1
GenericName=test script good v1
Icon=system-run
MimeType=
Name[en_US]=test script good v1
Name=test script good v1
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=

see : https://paste.opensuse.org/2919112

Is there any parser/converter to convert a multiline piece of code into one line code ?

Any help is welcome.

Put an question for the IP in the script.
Save the script and start the script in the desktop file by running it as script?

It would probably be easier to change the exec line of the “.desktop” file, so that it just calls your script.

As I said, the part of the exec command run correctly in konsole.

To answer your demand :

#
#!/bin/bash
#
IP_ADDR="192.168.130.239" 
while : ; do  
    ping -c2 "$IP_ADDR" 
    if  $? -eq 0 ] ; then 
        kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive" 
        break 
    else 
        kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?" 
         $? -eq 1 ] && break 
        sleep 2 
    fi 
done

see : https://paste.opensuse.org/36493563

Don’t focus on the example.
As I said, the command in the ‘exec’ key is trivial, but serves to clarify my question:
Is there a tool to convert a piece of multi-line code into a mono line acceptable for the exec key as I am really not an expert on bash syntax.

A “.desktop” file is for running simple commands (with possible arguments).

For anything more complex, use a script and use the “.desktop” file to run the script.

#
#!/bin/bash
#
IP_ADDR="192.168.130.239" 
while : ; do  
    ping -c2 "$IP_ADDR" 
    if  $? -eq 0 ] ; then 
        kdialog --title="Connexion to $IP_ADDR" --msgbox="Router is alive" 
        break 
    else 
        kdialog --title "Connexion to $IP_ADDR" --warningcontinuecancel "No connection

Are you sure you want to continue pinging ?" 
         $? -eq 1 ] && break 
        sleep 2 
    fi 
done

I know you do not like me giving no help. But the above is wrong. The shebang #!/bin/bash should be the FIRST line!!!

Okay, but that’s too bad.

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

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:

#!/usr/bin/bash

Your program is ready now. Next step is the .desktop file. I will only talk about the Exec = entry:

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:

/usr/bin/xdg-su -c /sbin/yast2

So I assume that something like

/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.

I am sorry but you did not read the question carefully.

I search if there is somewhere a syntax parser/converter to convert multi-ligne ‘if-the-else’ short piece of code to a one line stanza.
What the example does is of no interest. It is just an ‘if-the-else’.
As I said in my question, the example is given to clarify my question about converting multi-line to one line.

My question has nothing to do with root, xdg-su, chmod, Cobol, Ruby.

I must admit that this is a lot more polite then your “this is no help” from earlier times.

Wishing success.