Thursday, January 31, 2008

For cycle from shell

Let's see a small example of a for cycle directly inside the shell:

me@ubuntu:~/workspace/myFile$ for file in *
> do
> echo $file
> grep foo $file
> done
test.txt
hi, my name is foo

In this case we will cycle all the files in the current directory, print out their names and grep "foo" for each one. Of course you can simply make recursive grep with * as file input parameter... but this can be a startup point for more complex operations

No comments: