-   Linux - Newbie (
-   -   Bash programming (

winterhunter 11-23-2005 06:48 AM

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];


kilgoretrout 11-23-2005 07:17 AM

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:

theYinYeti 11-23-2005 07:47 AM

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


winterhunter 11-23-2005 07:58 AM

Thanks a lot! Merci beaucoup!

theYinYeti 11-24-2005 03:18 AM

De rien :) You're welcome!

gabriella 11-26-2007 07:01 AM

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!

echo setting up variables
rm temp.* temp2.* temp3.* temp4*


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

proc2d temp.hed temp3.hed
rm temp.* temp2.*
if [ $y1 -gt 0 ]; then y1=y1-1
if [ $y1 -eq 0 ]; then break
let rot=0

matthewg42 11-26-2007 07:14 AM

xargs can be told to work one file at a time like this:

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:

for f in *.whatever; do yourcommand "$f"; done

All times are GMT -5. The time now is 01:46 AM.