LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2005, 07:48 AM   #1
winterhunter
Member
 
Registered: Oct 2005
Distribution: Ubuntu Edgy
Posts: 58

Rep: Reputation: 15
Bash programming


Hi everyone,

I have a little problem: I have around two hundred files that must be treated individually (like "command file1", "command file2").

Of course, the program that must treat them doesn't accept wildcards. Is there any simple way of doing a "ls file*" and then redirecting its output line by line to the command I must run. Something that would look like this (in an imagined, C-like language):

filelist= ls; // suppossing that the outpout is an array of strings.
for(i=0;i<lenght(filelist);i++) {
command filelist[i];
}

Thanks!
 
Old 11-23-2005, 08:17 AM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 3,006

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
The xargs command is used to do that in bash programing. It takes the output of one command and pipes it to a new command one at a time. Check out this link for a nice practical discussion:

http://www.linuxlaboratory.org/30.php
 
Old 11-23-2005, 08:47 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Absolutely not! The xargs command takes multiple "words" from standard input and gives them all as arguments so a single command; example:

find . -name *.java | xargs tar -cf backup.tar

What you need is a loop. Assuming you may have spaces in some filenames (in which case cutting into shell words doesn't work), you'd have to run something like this:

ls -1 | while read filename; do command "$filename"; done

Yves.
 
Old 11-23-2005, 08:58 AM   #4
winterhunter
Member
 
Registered: Oct 2005
Distribution: Ubuntu Edgy
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks a lot! Merci beaucoup!
 
Old 11-24-2005, 04:18 AM   #5
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
De rien You're welcome!
 
Old 11-26-2007, 08:01 AM   #6
gabriella
LQ Newbie
 
Registered: Nov 2007
Posts: 1

Rep: Reputation: 0
bash programming

Hiya guys,
I hope somebody can help me,I am in a trouble with this program.
I tried to do loop-in-loop,but it didn't work and I don't know how can I do it.I have read documentation about it but I couldn't find solution for my problem.
We try to cut a part from a picture,and the first loop worked,but if we have more pictures and want to do the same with the others how can I do it?
how can I make variable the img?

Thanks for any help in advance!
Gabi



echo setting up variables
rm temp.* temp2.* temp3.* temp4*
filenm=hello
outfile=goodbye
img=0
rot=0

y1=61
y2=-$y1

proc2d $filenm.hed temp.hed first=0 last=0
while [ $rot -lt 360 ]; do
echo this will produce the circle - the hard way
proc2d temp.hed temp0.hed trans=0,$y1
proc2d temp0.hed temp2.hed trans=0,$y2
rm temp.* temp0.*
proc2d temp2.hed temp.hed rot=$rot
rm temp2.*
let rot=rot+2
done

proc2d temp.hed temp3.hed
rm temp.* temp2.*
if [ $y1 -gt 0 ]; then y1=y1-1
fi
if [ $y1 -eq 0 ]; then break
fi
let rot=0
 
Old 11-26-2007, 08:14 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
xargs can be told to work one file at a time like this:
Code:
find /path/to/files -type f | xargs -n 1 yourcommand
If all the files are in a single directory, why not use a for loop:
Code:
for f in *.whatever; do yourcommand "$f"; done
 
  


Reply

Tags
find, loop, shell, xargs


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash Programming moodupani Programming 1 08-31-2005 07:50 PM
bash programming alaios Programming 2 07-23-2005 01:52 AM
bash programming drisay Programming 6 12-20-2004 10:09 AM
bash programming cpukiller Linux - Newbie 1 11-04-2003 07:45 PM
bash programming bulliver Programming 2 04-05-2003 10:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:42 AM.

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