emilford has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to using Linux and have found that converting mp3's to wav's and then burning them to disc isn't nearly as easy as it was in Windows. But hey, that's part of the fun.

I've found a bunch of shell commands that will convert and burn, but entering each one in seperately is a pain. I'm trying to write a PERL script that I can use to automate this a bit.

My problem is when dealing with the file names. I use the readdir() command to grab all of the file names in a given directory. That part works fine, but when I attemp to issue commands based on the taken file names, the shell complains about some of them. Here is the error I get when just attempting to echo the file name to the screen:

original file name:
alicia keys - 11 - mr man(feat jimmy cozier) - simplemp3s.mp3

sh: -c: line 1: syntax error near unexpected token `way(i'
sh: -c: line 1: `echo alicia\ keys\ -\ 12\ -\ never\ felt\ this\ way(inter\ -\ simplemp3s.mp3'

It seems like it only complains about files with abnormal chars in them. How would I go about escaping both the spaces and the abnormal characters? Something like URI::Escape, but with \'s instead of %'s.

So the sum up:

1) How would I go about escaping specific chars and spaces with a \ ?
2) Which characters in a UNIX file name must be escaped?
3) And finally, after everything is coded and functioning, how would I go about making it excuteable from /usr/bin (or wherever) so that i can just call the name of the program and have it run?

Thanks in advance!

Replies are listed 'Best First'.
Re: Escaping characters in a file name
by belg4mit (Prior) on Dec 07, 2001 at 21:50 UTC
    See the quotemeta function in the perlfunc man page. But you might just look at http://freshmeat.net first. Perhaps clean something up instead of starting from scratch.

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Escaping characters in a file name
by Fletch (Bishop) on Dec 07, 2001 at 21:59 UTC

    Don't use the one argument form of system, pass it a list of the arguments instead. This will avoid starting a shell to parse things and you won't need to worry about spaces (or quotes, or semicolons, or . . . ) in filenames. C.f. perldoc -f quotemeta if you're bound and determined to get the shell involved.

    Of course, you could just use something like grip or any of the other existing utilities listed in freshmeat.net's CD Ripping category rather than re-inventing the wheel.

Re: Escaping characters in a file name
by Aighearach (Initiate) on Dec 07, 2001 at 22:08 UTC

    Oooops, you forgot to post your code.

    1) How would I go about escaping specific chars and spaces with a \ ?

    Generally with single quotes, rather than character-specific escaping with \. 'this is a single filename'. But that's in the shell, not in Perl... so in Perl you might want single quotes inside your Perl level quotes, something like: </code> system( "/usr/bin/myfavcoverter -flag 'my filename with lame spaces' '$my_scalar_that_holds_a_evil_file_with_funny_characters'" ); </code> Note that it's actually better to split up the arguments to system(), so that it doesn't have to use the shell... please see the documentation for system() for details.

    please don't do like I did there and use a scalar that was set from outside the file, like a filename or user input, until you've read and understand all of the perlsec man page( perldoc perlsec ). Otherwise, you'll be creating a security hole. But for converting media files, it's probably not really an issue, unless you're going to allow others to use it. For example, placing it in /usr/bin on a system with other users...

    2) Which characters in a UNIX file name must be escaped?

    That depends on which shell you're using. /bin/sh is usually bash, so "man bash" will help there. Or, just put it all in single quotes to cheat until you learn the details... ;)

    3) And finally, after everything is coded and functioning, how would I go about making it excuteable from /usr/bin (or wherever) so that i can just call the name of the program and have it run?

    assuming the first line of your Perl program is  #!/usr/bin/perl then you'll only need to do chmod a+x myperlscript

    update: I use the program xcdroast for all my linux cd burning joys. You might want to also look at gtoaster.


    --
    Snazzy tagline here
Re: Escaping characters in a file name
by thraxil (Prior) on Dec 07, 2001 at 22:12 UTC

    3) And finally, after everything is coded and functioning, how would I go about making it excuteable from /usr/bin (or wherever) so that i can just call the name of the program and have it run?

    it has to be executable and in your PATH.

    put it in /usr/local/bin and make sure it is executable (chmod +x file.pl). if you don't have root (which you should need to put something in /usr/local/bin), put it in a subdirectory of your home dir (such as ~/bin/) and add that directory to your PATH by adding export PATH="$PATH:~/bin" to the end of your .bashrc.

    anders pearson

Re: Escaping characters in a file name
by maverick (Curate) on Dec 08, 2001 at 00:29 UTC
    As an alternative have you looked at grip? it's a GUI front end to cdparanoia and several different encoders. It does cddb lookups on the cd's, rips and ecodes in parallel, etc. I've ripped and encoded my entire cd collection with it.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"