Skip to content
Advertisement

Linux piping data containing backticks

I’m piping output from one command into a second:

mpc listall | mpc add

mpc listall returns the following data (can output 1 or more lines):

Dare - 16 - I´ll Be Your King.mp3

When piping it into the next command, it seems that my shell (Ash on BusyBox) converts the ´ into an asterisk, as I get the error

error adding Dare - 16 - I*ll Be Your King.mp3: No such directory

Manually adding double quotes works! like this:

mpc add "Dare - 16 - I´ll Be Your King.mp3"

So, I tried adding those by sed and awk, but in those cases the backtick gets converted to an asterisk again:

mpc listall | sed 's/^/"/;s/$/"/'

returns:

"Dare - 16 - I*ll Be Your King.mp3"

So, question is, is there a way to pass backticks, or actually any character, as is, into another command without conversions?

By the way, obviously, it’s quite bad to have this character in the file name in the first place, but I want my code to be robust and able to handle anything thrown at it.

Advertisement

Answer

Not a direct solution the problem, but a work-around (that’s actually better, because it uses the API) in python using the python-mpd2 library:

myMpdClient = MPDClient() 
myMpdClient.connect("localhost", 6600)  # connect to localhost:6600
myMpdClient.findadd('base','directory-name')
myMpdClient.close()

I’ll accept a working solution to the actual proposed problem, instead of this one, if someone posts a working solution.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement