Howdy, I'm a total newbie to scripting and I can't figure out the proper syntax to shell script a for loop for the following scenario: I have a directory of symbolic links to backup log files. I want to loop through the dir to cat the last 21 lines of each file and put all the output to another file. I can't seem to get the loop to do what I want no matter how many different iterations of cat list | tail -21 etc. I try. I've looked through all my available books and on the web but so far no luck in finding just the right combo. Many thanks in advance for any help.

try it, post it, and we will help to correct it

First off, which "shell" are you talking about?
There are many varieties (sh, bash, ksh, csh) and syntax varies.

Maybe for i in `cat list`; do tail -21 $i; done

If all that's in that directory is the sym links to the files that you want to tail, try something like this: for i in $(ls /path/to/directory); do tail -n 21 $i; done Let us know how it goes! If we're missing the gist of what you're trying to do, post your script for us so that we can get a better idea!

Hope this helps!
-G

Thanks Salem, got an error (invalid option --2) when I tried yours - using sh as my shell. Masijade, here's what I've tried:

list=ls
for list
do 'cat list' | tail -21 > logfile
done

list in my example was a file, not a command.

> I want to loop through the dir to cat the last 21 lines of each file and put all the output to another file for i in *; do tail -21 $i; done Or begin with for i in *; do echo $i; done just to see what the for loop would use for filenames

Thanks all. Gromit's code suggestion works, I appended an output to a file ( > logfile) at the end to store the results for later viewing. I wanted to be able to identifiy each of the 21 line entries outputted according to its sym link (the server where it originated) so I also added an echo $i as:

for i in $(ls /path/to/directory); do tail -n 21 $i; echo $i; done > logfile

Thanks again for all your help!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.