(Perl?)Script needed
BEWARE: :newbie:
Hi, I'm looking for a script that will apply a certain command line of LAME on all files in a folder (I heard this could be easily done with Perl, but I guess any other language might be sufficient as well), so it would be cool if someone could give me the code or some useful FAQs to get into scripting. :) |
If the filenames do not contain spaces, it is as easy with bash as this:
directory=/path/to/directory lamecommand=somelamecommand for filename in `find $directory -type f` ; do $lamecommand $filename done If the filenames contain spaces, this does not work as not only the newline, but the space character is also treated as a field separator for the bash 'for' statement, thus filenames may be cut into fragments. Therefore I never use spaces in filenames. So far I could only find a nasty workarount to this problem: I replace spaces with some other character(s) by sed before the 'for' statement sees the filenames, then put them back before using the filename: for badfilename in `find $directory -type f | sed s! !@@!g` ; do filename=`echo $badfilename | sed s!@@! !g` $lamecommand "$filename" done In the above example I replaced each space with two characters: @@, then replaced them back. P.S. Since the exclamation mark is used in the sed command as an internal command separator, filenames should not contain '!' either :-(. If this is a problem, you can replace the exclamation mark in the sed command with any other character that is unlikely to be used in filenames. |
|
If you want to use J_Szucs's example then you can set the field seperator variable "IFS" to only be a newline by adding the following 2 lines above the "for" line.
IFS=" " |
Wow, I learned something new again, and that IFS trick is really useful.
However, does it apply to the "for" command only, or to the shell itself? In the first case my script needs some more tweaking: in order that lame treats the filename as one single parameter, the filename should be quoted by " or ', i.e. these characters should be also included in the command. |
It is a variable that is valid for a few things. I *think* awk uses it as it's default field seperators too.
|
All times are GMT -5. The time now is 10:22 AM. |