You are not logged in.
I'm writing a little script in bash that displays my current MPD music database in ratmen.
Ratmen is a very simple menu for Ratpoison, that works in this way: it accepts a menu item and a command associated with it, and when you want to use multiple items, you have to set up a delimiter between the item and its command, in this way:
ratmen -d?? "item??command" "item??command" "item??command"
As you can see in the example, the separator is "??".
Now, what I have done is creating a script that dynamically generates the list of my album folders and creates a command to play them, so that as a final result I have (more or less):
ratmen -d?? "Album Title??mpc ls Album Title | mpc play" "Album Title??mpc ls Album Title | mpc play"
Trivial, isn't it?
So far so good. I have made extensive use of sed, since I don't know much about programming. Now my problem is with single quotes in albums, it really messes up everything, and all the albums with a single quote inside do not work.
So, this is the script:
#!/bin/bash
mpc ls | sed -e "s/\(.*\)/\1??mpc clear; mpc ls \'\1\' | mpc add; mpc play/" | sed -e 's/\(.*\)/" \1 " /' | sed ':a;N;$!ba;s/\n//g' | sed 's/^/ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 /' > ./ratpoison-scripts/mpcdump;
chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratmen-scripts/mpcdump"
Whenever I run it, it creates a new file, called mpcdump and executes it. This file contains the actual ratmen command with arguments, like I have indicated above. The main problem is that I already use single quotes to quote the album titles, and double quotes to separate the ratmen arguments, like this:
ratmen -d?? "Album title??mpc play; mpc ls 'Album Title' | mpc play"
I have found a way of adding a single quote using escapes (using '\''), but it messes up my titles in the menu, since it doesn't get escaped in them. So I need a way to only edit the second part of the argument, namely only the title that appears in single quotes.
I hope this is clear enough, and I'm posting a screenshot to make it even clearer:
I have not replaced the single quotes with '\'' yet, since it's too messed up, and I prefer not to be able to select albums than having it all messed up
So, do you think I can do this?
Thanks
Last edited by finferflu (2008-01-15 00:33:59)
Have you Syued today?
Free music for free people! | Earthlings
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery
Offline
EDIT - see next post
Last edited by Cerebral (2008-01-14 20:15:42)
Offline
Tested - this should work for you
#!/bin/bash
rm ./ratpoison-scripts/mpcdump;
echo "ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 \\" >> ./ratpoison-scripts/mpcdump
mpc ls | sed "
# Copy title to hold buffer
h
# Escape single quotes
s#'#\\\\'#g
# Add ending
s#\(.*\)#\1' | mpc add; mpc play \" \\\\#
# Swap hold and pattern buffers
x
# Add beginning
s#\(.*\)#\t\" \1??mpc clear; mpc ls '#
# Get the end
G
# Remove any funny newlines
s#\n##g
" >> ./ratpoison-scripts/mpcdump;
chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratmen-scripts/mpcdump"
Offline
Thanks a lot for your effort, Cerebral
Unfortunately it doesn't work
this is the not so unusal error I get:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
And this is the command it generates when I select an album (there is an option in ratmen to only show the command):
mpc clear; mpc ls '2 Orchestra - Mercutio\'s Dead' | mpc add; mpc play
So it seems unhappy with \'. I have tried to only run the actual generated command and this is what I get at the prompt:
~ > mpc ls '2 Orchestra - Mercutio\'s Dead' | mpc add; mpc play
>
so it really seems it doesn't escape it.
Have you Syued today?
Free music for free people! | Earthlings
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery
Offline
You get that error when you click on the items in your list, you mean?
Try replacing
# Escape single quotes
s#'#\\\\'#g
in the script I gave with
# Escape single quotes
s#'#\\\\\\\\'#g
(... so ugly, but it adds another \)
Offline
Yes, I get it when I click on them. And as I have shown you, if you type the command generated by your script on the command line, it doesn't get executed.
I have added the extra backslashes, but I get the same error, I think the quotes don't get escaped despite the backslash...
Thanks for coming down into this quotes nightmare with me
Have you Syued today?
Free music for free people! | Earthlings
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery
Offline
Oh... yeah, silly me. Single quotes are screwing you over here, since the stuff between them is considered literal. Try this:
edit - won't work. BAH. Dumb quotes.
Last edited by Cerebral (2008-01-14 21:17:11)
Offline
ohwait... that won't work either. give me a sec
Offline
#!/bin/bash
rm ./ratpoison-scripts/mpcdump;
echo "ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 \\" >> ./ratpoison-scripts/mpcdump
mpc ls | sed "
# Copy title to hold buffer
h
# Escape single quotes
s#'#'\"'\"'#g
# Add ending
s#\(.*\)#\1' | mpc add; mpc play \" \\\\#
# Swap hold and pattern buffers
x
# Add beginning
s#\(.*\)#\t\" \1??mpc clear; mpc ls '#
# Get the end
G
# Remove any funny newlines
s#\n##g
" >> ./ratpoison-scripts/mpcdump;
chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratpoison-scripts/mpcdump"
Test?
Offline
Yeah, welcome to my nightmare
It's still not working
> ./mpclist
~/ratpoison-scripts/mpcdump: line 99: unexpected EOF while looking for matching `"'
~/ratpoison-scripts/mpcdump: line 100: syntax error: unexpected end of file
Edit:
Ok, I opened ./ratpoison-scripts/mpcdump, and I saw how one of the lines was rendered:
" 2 Orchestra - Mercutio's Dead??mpc clear; mpc ls '2 Orchestra - Mercutio'"'"'s Dead' | mpc add; mpc play "
I guess it doesn't like that either...
Last edited by finferflu (2008-01-14 21:43:34)
Have you Syued today?
Free music for free people! | Earthlings
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery
Offline
DOH. One more time.
#!/bin/bash
rm ./ratpoison-scripts/mpcdump;
echo "ratmen -d?? --title \"MPD Database\" --foreground beige --background gray33 \\" >> ./ratpoison-scripts/mpcdump
mpc ls | sed "
# Copy title to hold buffer
h
# Escape single quotes
s#'#'\\\\\"'\\\\\"'#g
# Add ending
s#\(.*\)#\1' | mpc add; mpc play \" \\\\#
# Swap hold and pattern buffers
x
# Add beginning
s#\(.*\)#\t\" \1??mpc clear; mpc ls '#
# Get the end
G
# Remove any funny newlines
s#\n##g
" >> ./ratpoison-scripts/mpcdump;
chmod +x "./ratpoison-scripts/mpcdump";
exec "./ratpoison-scripts/mpcdump"
Offline
Yeah! you did it!
I wish we had a triple quote sometims
Anyway I have learnt the hold buffer technique thanks to your help.
Thanks a lot!
Have you Syued today?
Free music for free people! | Earthlings
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery
Offline