Entries Comments



The for Loop

7 December, 2007 (05:14) | commands, UNIX | By: passion@linux

The for loop is used to process a list of items. The list can be supplied in the script. It could also be taken from the script arguments, from a file, or from the output of a command.

The syntax of the for construct is as follows:

for list
do
command_list
done

All the commands in the list are carried out for each item in the list.

The following script invokes the C compiler cc for all files with the extension of .c held in the current directory.


for filename in $(ls *.4gl)
do
cc $filename
done

A similar script could be written to compile several programs specified on the command line. The user can specify as many programs as he wants.


for filename in $*
do
cc $filename
done

$* expands to all the arguments given by the user. Each argument will count as a single item in the list.

This construct is very useful for working with commands that give more than one line of output. Note, however, that the loop will be carried out for each word of the output, rather than once for each line. Commands whose output consist of several columns separated by spaces will be treated as one list item for each column of each row.

To illustrate this, we will look at a script that takes the output from the ps command, and attempts to process it as a list. The output from ps looks something like this:

PID TTY TIME CMD
3218 pts/2 0:01 ksh
3207 pts/2 0:00 sh
3550 pts/2 0:00 ps

The script is as follows:


for process in $(ps)
do
echo $process
done

and gives the following output:

PID
TTY
TIME
CMD
3218
pts/2
0:01
ksh
3207
pts/2
0:00
sh
3579
pts/2
0:00
ps
3578
pts/2
0:00
ksh

If we wanted to carry out commands on each line rather than on each word, and ignore the column headings, we would have to use some ingenuity. The following script displays the PID and command from each line output from the ps command.

компютри


count1=0
count2=0
for process in $(ps)
do
count1=$(($count1 + 1))
if test $count1 -lt 5
then
continue
fi
count2=$(($count2 + 1))

if test $count2 -eq 1
then
pid=$process
fi
if test $count2 -eq 4
then
cmd=$process
echo $pid $cmd
count2=0
fi
done

The output would look like this:

3218 ksh
3207 sh
3609 ps
3608 ksh

The script uses two counters. The first, count1, is used to skip out the headings. The first four items in the list are headings, so if the counter is less than 5, it uses the continue command to skip all further commands for that item. The second counter is used to establish which column of the ps output is being processed. When it is column 1, it stores the PID in a variable. For column 4, it stores the command in a variable, echoes both variables to the user, and resets the count to zero.

free payday loanlow cost payday loancash advance payday loanapproval guaranteed loan paydaypayday loan applicationyahoo payday loanmoney tree payday loanloan money payday treepayday loan toronto,payday loan in toronto,loan payday torontoloan payday yahooinstant approval payday loan,approval instant loan paydaypayday loan paycheck advancecredit loan no payday,credit dollar loan no payday ten,no credit payday loanone hour payday loanno credit check payday loancalgary loan payday,calgary payday loanfast online payday loanfast loan online paydayadvance loan payday quickquick payday advance loanmilitary payday loanno fax cash advance,advance cash fax no,account advance cash fax no savingadvance cash company scams,cash advance company,advance advance cash company heirpayday payday loan cash advance loan,advance cash loan payday today,advance cash loan paydayadvance cash loan onlineace cash advancecash fast loan paydayfast faxless cash advance,fast cash advance,advance cash fast faxing nonational cash advancepayday us fast cash loan,fast cash payday loanadvance america cash company,advance america cash loan,cash advance americacash loan payday untiladvance cash loan loan paydayadvance america cashcash advance payday loan softwarebusiness cash advance loan,business cash advance,how to start a cash advance businessadvance cash loan payday softwareadvance cash cashing check greenville,greenville,advance cash cashing check durham,durham,check cash advancefirst american cash advancecash until payday loanadvance cash overnightcash advance servicecash loan payday tilfirst time cash advance,first choice cash advance,first cash advancescams on cash advance company,advance cash companyadvance cash now quick,quick cash advance,quick no fax cash advancecash in advancecash advance loan illinois,5 advance cash illinois loanadvance cash check credit noace advance america cash,advance america cash first,advance america cash advance free ringtones maker | nextel ringtones cd | free cingular mp3 ringtones | free real ringtones sprint | cell free phone ringtones verizon wireless | free crazy frog ringtones download | free hindi ringtones | download polyphonic ringtones | free t mobile ringtones | verizon lg ringtones | free mp3 ringtones for t mobile | free cellular phone ringtones | download midi ringtones | mp3 ringtones creator | us cellular ringtones | info personal polyphonic remember ringtones | free phone ringtones | absolutely free ringtones | make your own mp3 ringtones | blue tooth free ringtones |

Write a comment