Free Hosting Online for WorkStations

< Previous | Contents | Next >

for: Traditional Shell Form

The original for command’s syntax is:

for variable [in words]; do

commands

done

Where variable is the name of a variable that will increment during the execution of the loop, words is an optional list of items that will be sequentially assigned to variable, and commands are the commands that are to be executed on each iteration of the loop.

The for command is useful on the command line. We can easily demonstrate how it works:



[me@linuxbox ~]$ for i in A B C D; do echo $i; done

A B C D

[me@linuxbox ~]$ for i in A B C D; do echo $i; done

A B C D


In this example, for is given a list of four words: “A”, “B”, “C”, and “D”. With a list of four words, the loop is executed four times. Each time the loop is executed, a word is as- signed to the variable i. Inside the loop, we have an echo command that displays the value of i to show the assignment. As with the while and until loops, the done key- word closes the loop.


The really powerful feature of for is the number of interesting ways we can create the list of words. For example, through brace expansion:



[me@linuxbox ~]$ for i in {A..D}; do echo $i; done

A B C D

[me@linuxbox ~]$ for i in {A..D}; do echo $i; done

A B C D


or pathname expansion:



[me@linuxbox ~]$ for i in distros*.txt; do echo $i; done

distros-by-date.txt distros-dates.txt distros-key-names.txt distros-key-vernums.txt distros-names.txt distros.txt

distros-vernums.txt distros-versions.txt

[me@linuxbox ~]$ for i in distros*.txt; do echo $i; done

distros-by-date.txt distros-dates.txt distros-key-names.txt distros-key-vernums.txt distros-names.txt distros.txt

distros-vernums.txt distros-versions.txt


or command substitution:



#!/bin/bash

# longest-word: find longest string in a file while [[ -n $1 ]]; do

if [[ -r $1 ]]; then max_word= max_len=0

for i in $(strings $1); do len=$(echo -n $i | wc -c)

if (( len > max_len )); then max_len=$len max_word=$i

fi

done

echo "$1: '$max_word' ($max_len characters)"

fi shift

done

#!/bin/bash

# longest-word: find longest string in a file while [[ -n $1 ]]; do

if [[ -r $1 ]]; then max_word= max_len=0

for i in $(strings $1); do len=$(echo -n $i | wc -c)

if (( len > max_len )); then max_len=$len max_word=$i

fi

done

echo "$1: '$max_word' ($max_len characters)"

fi shift

done

for: Traditional Shell Form


In this example, we look for the longest string found within a file. When given one or more filenames on the command line, this program uses the strings program (which is included in the GNU binutils package) to generate a list of readable text “words” in each file. The for loop processes each word in turn and determines if the current word is the longest found so far. When the loop concludes, the longest word is displayed.

If the optional in words portion of the for command is omitted, for defaults to pro- cessing the positional parameters. We will modify our longest-word script to use this method:



#!/bin/bash

# longest-word2: find longest string in a file for i; do

if [[ -r $i ]]; then max_word= max_len=0

for j in $(strings $i); do len=$(echo -n $j | wc -c)

if (( len > max_len )); then max_len=$len max_word=$j

fi

done

echo "$i: '$max_word' ($max_len characters)"

fi done

#!/bin/bash

# longest-word2: find longest string in a file for i; do

if [[ -r $i ]]; then max_word= max_len=0

for j in $(strings $i); do len=$(echo -n $j | wc -c)

if (( len > max_len )); then max_len=$len max_word=$j

fi

done

echo "$i: '$max_word' ($max_len characters)"

fi done


As we can see, we have changed the outermost loop to use for in place of while. By omitting the list of words in the for command, the positional parameters are used in- stead. Inside the loop, previous instances of the variable i have been changed to the vari- able j. The use of shift has also been eliminated.


Why i?

You may have noticed that the variable i was chosen for each of the for loop examples above. Why? No specific reason actually, besides tradition. The variable used with for can be any valid variable, but i is the most common, followed by j and k.



The basis of this tradition comes from the Fortran programming language. In For- tran, undeclared variables starting with the letters I, J, K, L, and M are automati- cally typed as integers, while variables beginning with any other letter are typed as real (numbers with decimal fractions). This behavior led programmers to use the variables I, J, and K for loop variables, since it was less work to use them when a temporary variable (as loop variables often are) was needed.

It also led to the following Fortran-based witticism: “GOD is real, unless declared integer.”


Top OS Cloud Computing at OnWorks: