ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.