0

I have a desktop file for a GTK application that I have created which has the following structure:

[Desktop Entry]
Version=1.0
Name=My GTK Application
Comment=Desktop File
Exec=bash -c 'sudo taskset -c 6 /home/user_name/path/to/app/./executable %f 2>> /home/user_name/path/to/app/LogFiles/stderr.log'
Icon=/home/user_name/path/to/app/Images/icon.png
Terminal=false
Type=Application
Categories=Utility;Application;
MimeType=application/x-myapp;
Name[en_US]=GTK Application

As you can see, I have a mime type association with the application such that files with the proper extension can be double clicked and opened in the app. The absolute path of the file gets passed as a command line argument and the application (written in C) captures argv[1] (when it isn't NULL) and uses that to open the file. The problem is that this fails when there is white space in the file name. The original Exec line that I had was simply:

Exec=sudo taskset -c 6 /home/user_name/path/to/app/./executable

Since I wasn't trying to redirect stderr to a logfile. With this exec line, I did not need %f on the line in order for the C program to grab argv[1] and file names with white space were not an issue. I found that redirecting stderr on the Exec line only worked when I used bash -c, which according to the man page, "executes the commands from a string." Now I cannot capture the file unless %f is used, but it fails when there is white space in the file name. I have tried to use single-quotes, double-quotes, and an endless combination of other things to no avail. I know of two work-arounds for this: (1) I could have the Exec line execute a script instead, or (2) if I use %F instead, then all of the parts of the file name are passed to argv[2], argv[3] et cetera, depending on the number of white spaces in the file name. I would then just have to use argc[] and loop through to capture all of the argv[] to concatenate everything back together into the actual file name. Neither of these options seem eloquent. There has to be a better way that I am missing. If there is a way to redirect stderr without bash -c, that might be a solution. Or perhaps there is a bash command like sed or tee that can be used? Basically, I just want an Exec line ONLY that will capture a filename with white space into argv[1] and still allow stderr to redirect to a file.

1

You can pass the %f argument as a positional parameter to the shell by placing it after your command string, and quote it inside the command string in the usual way.

Ex.

Exec=sh -c 'somecommand "$1" 2>> /path/to/stderr.log' sh %f

This technique even works with the %F multi-file placeholder:

Exec=sh -c 'somecommand "$@" 2>> /path/to/stderr.log' sh %F

I'm using sh not bash here since there doesn't seem to be anything bash-specific about your command, however the same applies in bash:

       -c        If the -c option is present, then commands are read from  the
                 first non-option argument command_string.  If there are argu‐
                 ments  after  the  command_string,  the  first  argument   is
                 assigned  to  $0  and any remaining arguments are assigned to
                 the positional parameters.  The assignment  to  $0  sets  the
                 name  of  the  shell, which is used in warning and error mes‐
                 sages.
  • Works like a charm! Thank you! – Leigh K Nov 8 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.