Loops: the basics

simplest loops

command/output explanation
$ for i in a b c d e
> do
> echo $i
> done
a
b
c
d
e           
redefine $i successively as 'a', 'b', 'c', 'd', and 'e'

repeat the commands following 'do' for each value of $i

show the value of $i as we go

$ for i in `echo hello`
> do
> echo $i
> done
hello                   
Looping on values given by a command (issued inside
$ echo hello there world
hello there world
$ for i in `echo hello there world`
> do
> echo $i
> done
hello
there
world 
arguments given to loop variable are space delimited; each echo statement produces a new line.
$ echo this one>afile
$ for i in `cat afile`
> do
> echo $i
> done
this
one 
above remark is true even when loop variable is initialized by `cat filename`
$ cat sample
one 1 first
two 2 second
three 3 third
A file.
$ for i in `cat sample`
> do
> echo $i
> done
one
1
first
two
2
second
three
3
third
echoing the lines of the file in a loop
$for i in `cat sample|cut -d' ' -f2`
> do
> echo $i
> done
1
2
3 
Taking out the second word first. Is this the same as
cut -d' ' -f2 sample?

Why or why not?

grepping in a loop

command/output explanation
$ w=/homes/share/words-english
$ for i in a b c d e
> do
> grep ^$i.*by$ $w |wc
> echo $i
> done
      1       1       5
a
      3       3      17
b
      2       2      13
c
      1       1       6
d
      0       0       0
e                    
redefine $i successively as 'a', 'b', 'c', 'd', and 'e'

repeat the commands following 'do' for each value of $i

look for words in the dictionary beginning with  $i and ending with 'by'; count them

show the value of $i as we go

grep ^[abcde].*by$ $w
abby
baby
bobby
booby
chubby
cubby
derby       
verification that the output makes sense

extrapolation of the above:

command/output explanation
$ cat fancy
w=/homes/ddailey/public_html/words
for i in `grep ^$1 $w |sed s/$1//`
do
grep ^$i$ $w
done          
We take the string provided from the command line ($1) and look for words in the dictionary which begin with that string. We then remove the string itself (using sed) keeping only the suffix. We then look up that suffix in the dictionary and output only those suffixes which are stand-alone words.
$ ./fancy ham
ate
let
mock
per
string                        
After setting execute privs on the file fancy we can run it on given input.

Working in a loop with the lines of a file:

command/output explanation
$ cat fancy
w=/homes/ddailey/public_html/words
for i in `grep ^$1 $w |sed s/$1//`
do
grep ^$i$ $w
done          
We take the string provided from the command line ($1) and look for words in the dictionary which begin with that string. We then remove the string itself (using sed) keeping only the suffix. We then look up that suffix in the dictionary and output only those suffixes which are stand-alone words.
$ ./fancy ham
ate
let
mock
per
string                        
After setting execute privs on the file fancy we can run it on given input.

Querying web logs -- remember the discussion at http://www.sru.edu/depts/cisba/compsci/dailey/unix/weblogs.htm

command/output explanation
$ cat webstrings 
205 
dailey 
gif 
jpg
A file containing a list of strings to be searched in each of the log files.
for i in `cat webstrings`
do
echo ----------
echo *******$i
for j in 1 2 3 4
do
echo -----------
grep -c $i $log
echo '  'log.$j
done
done   
                      
A script which takes each string and looks it up (keeping a count) in each of the four logs.

extrapolation of the above:

command/output explanation
$ cat whtml
echo "<html><body><table border=1>"
for i in `cat webstrings`
do
echo "<tr><td></td>"
for j in 1 2 3 4
do
echo "<td>$j</td>"
done
echo "</tr><tr><td>$i</td>"
for j in 1 2 3 4
do
echo "<td>"
grep -c $i /etc/httpd/logs/access_log.$j
echo '  'log.$j
echo "</td>"
done
echo "</tr>"
done
echo "</table></body></html>"        
The same basic idea as above, but with fancier formatting of the output.
$ ./whtml >public_html/whtml.html  
Redirect the output into the "public_html" directory where it can be seen on the web.
http://marble.sru.edu/~ddailey/whtml.html
The web address.