LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   (Perl?)Script needed (https://www.linuxquestions.org/questions/programming-9/perl-script-needed-129358/)

Cyrus XIII 12-27-2003 06:30 PM

(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. :)

J_Szucs 12-27-2003 09:58 PM

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.

infamous41md 12-28-2003 12:58 AM

http://www.tldp.org/LDP/abs/html/

david_ross 12-28-2003 08:04 AM

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="
"

J_Szucs 12-28-2003 04:33 PM

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.

david_ross 01-03-2004 11:45 AM

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.