The for Loop
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.
\n\nThe syntax of the for construct is as follows:\n\nfor list\ndo\ncommand_list\ndone\n\nAll the commands in the list are carried out for each item in the list.\n\n
The following script invokes the C compiler cc for all files with the extension of .c held in the current directory.
\n\n\nfor filename in $(ls *.4gl)\ndo\ncc $filename\ndone\n\n\n
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.
\n\n\nfor filename in $*\ndo\ncc $filename\ndone\n\n\n$* expands to all the arguments given by the user. Each argument will count as a single item in the list.\n\n
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.
\n\n
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:
\n\n PID TTY TIME CMD\n 3218 pts/2 0:01 ksh\n 3207 pts/2 0:00 sh\n 3550 pts/2 0:00 ps\n\nThe script is as follows:\n\n\nfor process in $(ps)\ndo\necho $process\ndone\n\n\nand gives the following output:\n\nPID\nTTY\nTIME\nCMD\n3218\npts/2\n0:01\nksh\n3207\npts/2\n0:00\nsh\n3579\npts/2\n0:00\nps\n3578\npts/2\n0:00\nksh\n\n
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.
компютри\n\n\ncount1=0\ncount2=0\nfor process in $(ps)\ndo\ncount1=$(($count1 + 1))\nif test $count1 -lt 5\n then\n continue\nfi\ncount2=$(($count2 + 1))\n\nif test $count2 -eq 1\n then\n pid=$process\nfi\nif test $count2 -eq 4\n then\n cmd=$process\n echo $pid $cmd\n count2=0\nfi\ndone\n\n\nThe output would look like this:\n\n3218 ksh\n3207 sh\n3609 ps\n3608 ksh\n\n
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 loanмебели пловдивcash 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 |
