Page 1 of 2 12 LastLast
Results 1 to 10 of 11

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

  1. #1
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,346

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

    Code:
    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\n\nAre 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 :

    Code:
    [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\n\nAre 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 :
    Code:
    [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\n\nAre 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 :

    Code:
    [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\n\nAre 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.
    Thanks for helping. JCD
    __________
    server leap 15.3 -- ASUS g75vw KDE leap 15.3 -- ASUS G731GV KDE leap 15.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - HP 9012E

  2. #2
    Join Date
    Mar 2011
    Location
    Sauerland
    Posts
    7,317

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

    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?

  3. #3
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    16,047
    Blog Entries
    3

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

    It would probably be easier to change the exec line of the ".desktop" file, so that it just calls your script.
    openSUSE Leap 15.4; KDE Plasma 5.24.4;
    testing Tumbleweed.

  4. #4
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,346

    Default Re: AW: kde - syntax parser/converter for the exec key of .desktop file

    Quote Originally Posted by Sauerland View Post
    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?
    As I said, the part of the exec command run correctly in konsole.

    To answer your demand :
    Code:
    #
    #!/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\n\nAre you sure you want to continue pinging ?" 
            [ $? -eq 1 ] && break 
            sleep 2 
        fi 
    done
    see : https://paste.opensuse.org/36493563
    Thanks for helping. JCD
    __________
    server leap 15.3 -- ASUS g75vw KDE leap 15.3 -- ASUS G731GV KDE leap 15.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - HP 9012E

  5. #5
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,346

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

    Quote Originally Posted by nrickert View Post
    It would probably be easier to change the exec line of the ".desktop" file, so that it just calls your script.
    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.
    Thanks for helping. JCD
    __________
    server leap 15.3 -- ASUS g75vw KDE leap 15.3 -- ASUS G731GV KDE leap 15.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - HP 9012E

  6. #6
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    16,047
    Blog Entries
    3

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

    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.
    openSUSE Leap 15.4; KDE Plasma 5.24.4;
    testing Tumbleweed.

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    31,289

    Default Re: AW: kde - syntax parser/converter for the exec key of .desktop file

    Code:
    #
    #!/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\n\nAre 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!!!
    Henk van Velden

  8. #8
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,346

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

    Quote Originally Posted by nrickert View Post
    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.
    Okay, but that’s too bad.
    Thanks for helping. JCD
    __________
    server leap 15.3 -- ASUS g75vw KDE leap 15.3 -- ASUS G731GV KDE leap 15.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - HP 9012E

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    31,289

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

    Quote Originally Posted by jcdole View Post
    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
    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:
    Code:
    #!/usr/bin/bash
    
    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.
    Henk van Velden

  10. #10
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,346

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

    Quote Originally Posted by hcvv View Post
    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:
    Code:
    #!/usr/bin/bash
    
    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.
    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.
    Thanks for helping. JCD
    __________
    server leap 15.3 -- ASUS g75vw KDE leap 15.3 -- ASUS G731GV KDE leap 15.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - HP 9012E

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •