You are not logged in.

#1 2010-11-09 21:31:03

kailua
Member
Registered: 2010-05-27
Posts: 13

mpc in bash script

hi,

I am trying to write a little bash script for adding all songs with the same artist as the currently playing song. Here is what I already have:

artist=`mpc -f %artist% | head -n 1`
search=`mpc search artist "$artist" | sed 's|mp3|mp3"|g' | sed 's|ganze|"ganze|g'`
echo $search | mpc add

the sed commands are for enclosing all paths in the list by "
the point is that the last command doesn't work and I don't know why! if I type

echo $search

and copy the the output to the end of mpc add, mpd adds the songs to it's playlist. so my problem here is that mpc doesn't "eat" the content of the search-variable in the pipe. what am I doing wrong????

thx in advance

Offline

#2 2010-11-09 21:43:23

kittykatt
Member
From: Missouri, USA
Registered: 2009-11-04
Posts: 260
Website

Re: mpc in bash script

Have you thought of surrounding the $search variable in double quotes in the last line? If there are spaces involved in that variable, then you'll most likely need them.


- [ My Blog ] | [ AUR Packages ] | [ My deviantART ] | [ screenFetch ] | [ SilverIRC ] -

Offline

#3 2010-11-09 21:50:31

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: mpc in bash script

In theory, I guess it should work...

Maybe try this simpler one?

mpc search artist "$(mpc current -f %artist%)" | mpc add

Also, I tried when enclosing the paths in quotes (sed -r 's/(.*)/"\1"/' btw...) and mpc errored saying path not found.  Perhaps he's smart enough to break on '\n' only and not care about spaces.  I would try the above as-is and see how it goes.

Last edited by brisbin33 (2010-11-09 21:51:44)

Offline

#4 2010-11-09 22:00:49

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: mpc in bash script

kittykatt wrote:

Have you thought of surrounding the $search variable in double quotes in the last line? If there are spaces involved in that variable, then you'll most likely need them.

As far as I know, echo doesn't care about quotes unless you're trying to preserve whitespace at the beginning or end of the line.

# these are equivalent
var='my awesome variable'
echo $var
echo "$var"

# but these you need to be careful
var='  my awesome variable with whitespace   '
echo $var
echo "$var"

Last edited by brisbin33 (2010-11-09 22:01:26)

Offline

#5 2010-11-09 22:37:18

kittykatt
Member
From: Missouri, USA
Registered: 2009-11-04
Posts: 260
Website

Re: mpc in bash script

brisbin33 wrote:
kittykatt wrote:

Have you thought of surrounding the $search variable in double quotes in the last line? If there are spaces involved in that variable, then you'll most likely need them.

As far as I know, echo doesn't care about quotes unless you're trying to preserve whitespace at the beginning or end of the line.

# these are equivalent
var='my awesome variable'
echo $var
echo "$var"

# but these you need to be careful
var='  my awesome variable with whitespace   '
echo $var
echo "$var"

Touche. It's been a long day for me, unfortunately. Starting to make slip-ups like that.


- [ My Blog ] | [ AUR Packages ] | [ My deviantART ] | [ screenFetch ] | [ SilverIRC ] -

Offline

#6 2010-11-09 22:57:36

kailua
Member
Registered: 2010-05-27
Posts: 13

Re: mpc in bash script

hi,

thanks for your quick answers. finally the solution of brisbin33 worked very well. that's it how it should be: simple and short smile. so I dropped the complex thing with lots of variables.

so thanks and good night

Offline

Board footer

Powered by FluxBB