LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-19-2007, 08:31 PM   #1
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Rep: Reputation: 15
simple shell script to iterate the files


I want to write a shell script that will read the files in the directory and then i would need to each file (name) as the parameter to command (to run java class)

For(each file in the directory)
{
java testjava.java file_name
}
 
Old 02-19-2007, 08:50 PM   #2
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 449

Rep: Reputation: 34
Code:
for fname in $(ls) ; do
Doesn't work for files with spaces in the name though
 
Old 02-19-2007, 08:57 PM   #3
varunbihani
Member
 
Registered: Jul 2003
Location: indore, india
Posts: 69

Original Poster
Rep: Reputation: 15
Is it possible to do the ls to get only files with ".java" extension ?
Also, I would like to read the subdirectories ?
 
Old 02-19-2007, 09:05 PM   #4
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 449

Rep: Reputation: 34
Code:
for fname in $(ls *.java) ; do
Not sure about the sub-directories... Try the -R flag for ls which is recursive. I'm not sure if it will output it in the correct format for bash to interpret.
 
Old 02-19-2007, 09:07 PM   #5
tbutttbutt
Member
 
Registered: Jan 2006
Location: India
Distribution: Fedora 7, Linuxfromscratch 6.2
Posts: 66

Rep: Reputation: 15
You need a loop like:

for i in `ls`
do
case $i in
.) ;;
,,) ;;
*) java testjava.java $i;;
esac
done
 
Old 02-20-2007, 01:49 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by varunbihani
Also, I would like to read the subdirectories ?
The find command is more suitable for looking into subdirectories:
Code:
find . -type f -name "*.java" -exec your_command_here {} \;
where the option -exec execute your commands on each file found. It should take care of blank spaces inside filename, too. See man find for details.
 
Old 02-20-2007, 04:01 PM   #7
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 449

Rep: Reputation: 34
Thanks colucix - I didn't think of that
 
Old 02-21-2007, 05:27 AM   #8
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
To make "ls" work with spaces in filenames, just play around with the IFS variable.
By default, $IFS contains a space, a tab and a newline character, so output of ls is split on all those, including the spaces in the names of files.
Setting $IFS to only include the newline will avoid this.
You can restore $IFS to it's normal setting afterwards too.
Code:
backup=${IFS}
IFS="\n"
for i `ls`; do
...
done
IFS=${backup}
or something like that...

But in this case, the "find" command is indeed a better solution.
 
Old 02-21-2007, 05:55 AM   #9
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 449

Rep: Reputation: 34
I never knew that variable existed timmeke - cheers

BUT... :P
Setting it to only a new line would mean you'd have to alter the output of ls with the -l option wouldn't it? and then you would get all the other output (permissions, dates, size etc) that goes with the -l option...
 
Old 02-21-2007, 06:25 AM   #10
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
Quote:
Originally Posted by fukawi2
Setting it to only a new line would mean you'd have to alter the output of ls with the -l option wouldn't it?
The default ls command only produces output that goes across the line when it knows it's writing to a tty device (a terminal of some sort). When it's being used in scripts, with its output being used as here, it outputs in a column.

Try typing "ls | cat" on a terminal :-)
 
Old 02-21-2007, 03:35 PM   #11
fukawi2
Member
 
Registered: Oct 2006
Location: Melbourne, Australia
Distribution: ArchLinux, ArchServer, Fedora, CentOS
Posts: 449

Rep: Reputation: 34
Well butter my butt and call me a biscuit... Cool!
 
Old 02-22-2007, 01:23 AM   #12
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Or you can use the "-1" (that's number one, not the letter l) option of ls to force printing in one column.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Very simple shell script - move files to different directories beley Programming 7 11-02-2006 05:24 AM
Need a simple shell script please overwritting files. stefaandk Programming 9 10-11-2006 07:24 AM
how-to: repeat OR iterate shell OR bash command delay OR interval admarshall Linux - General 5 07-18-2005 10:47 PM
a simple shell script Warchief Programming 1 07-31-2003 05:01 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:10 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