LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2003, 06:30 PM   #1
Cyrus XIII
LQ Newbie
 
Registered: Dec 2003
Distribution: Ubuntu Hoary
Posts: 21

Rep: Reputation: 15
(Perl?)Script needed


BEWARE:

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.
 
Old 12-27-2003, 09:58 PM   #2
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
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.
 
Old 12-28-2003, 12:58 AM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
http://www.tldp.org/LDP/abs/html/
 
Old 12-28-2003, 08:04 AM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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="
"
 
Old 12-28-2003, 04:33 PM   #5
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
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.
 
Old 01-03-2004, 11:45 AM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
It is a variable that is valid for a few things. I *think* awk uses it as it's default field seperators too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl guru needed J_Szucs Programming 1 10-11-2004 03:05 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
perl(script) needed for pure-ftpd hobylinux Linux - Networking 0 08-26-2003 08:31 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration