j4-dmenu-desktop
j4-dmenu-desktop copied to clipboard
unable to escape the command correctly for dmenu
I'm running into the same problem as #68 , except I wasn't trying to hard code any colors. I want to create a script similar to dmenu_run which gets passed whatever command dmenu expects and it does the right thing. my initial attempt looks like this:
#!/bin/sh
exec /usr/bin/j4-dmenu-desktop --dmenu="dmenu $@" --usage-log="$HOME/.cache/desktop_run"
This causes problem since the line is executed by a shell which treats "#" as a comment. I tried some ways to escape it but without success. what should I do here? imo, using a shell to parse --dmenu= is too strong, since the user is probably invoking the command with a shell already so any parsing can be done beforehand.
My suggestion would be to change to take arguments like this
j4-dmenu-desktop -a -b -c --long-arg1 --long-arg2 -- dmenu -dmenu-arg1 -dmenu-arg2 -dmenu-arg3
i.e. after --
is the dmenu command that will be used.
This way the program can just directly exec the rest of the argv after --
and don't need to invoke the shell or do any parsing.
I see. Sort of a perfect-forwarding problem, not very elegant to solve, but possible.
#!/bin/sh
quoted=""
for arg in "$@"
do
quoted="$quoted $(set | grep '^arg=' | cut -d= -f2)"
done
j4-dmenu-desktop --dmenu="dmenu $quoted"
After much digging, I found this feature of bash (which means i need to use bash :\). it solves my exact problem, but obviously doesn't scale well to other escaping problems.
--dmenu="dmenu ${*//#/\\#}"