CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Programming] Shell Programming Series(IX)


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author System On 2007-04-29 10:01:47




View and vote on the article here: Shell Programming Series(IX)


Shell Programming Series(IX)

Category
Programming
Summary
The <TT>for</TT> Loop

The <TT>for</TT> loop is different from the <TT>while</TT> or <TT>until</TT> loops. Instead of evaluating a true/false test condition, the <TT>for</TT> loop simply
Body
When creating a <TT>for</TT> loop, you supply it a variable. Each iteration of the <TT>for</TT> loop changes the value of the variable to the next argument in the list. The <TT>for</TT> loop continues until the argument list has been exhausted, at which point the loop exists and the first statement after the loop is executed. The following program uses a <TT>for</TT> loop along with the <TT>bc</TT> command you learned about earlier to print the square root of all numbers from 10 to 20.

1.#!/bin/sh
2.# Print square roots of 10 -20
3.for num in ` jot 10 10 20`
4.do
5. square_root=`echo ?scale=5; sqrt($num)? | bc ?l`
6. echo $square_root
7.done
8.exit 0


The output from this program is as follows:

3.16227
3.31662
3.46410
3.60555
3.74165
4.00000
4.12310
4.24264
4.35889
4.47213


? Line 3: Line 3 of the program contains the <TT>for</TT> loop. It introduces a new command called <TT>jot</TT>. The <TT>jot</TT> command can do many useful operations with numbers, including printing a string of numbers and generating random numbers. In this case, we have used <TT>jot</TT> to print a string of 10 numbers, starting with number 10 and ending at number 20 (10 10 20). The <TT>for</TT> loop assigns each one of these values to the variable ?num? in sequence and then performs the statements in the body of the loop for each value.
? Line 5: This line uses the <TT>bc</TT> command to compute the square root of the value stored in $num. The ?scale=5? statement tells <TT>bc</TT> that the output should be scaled to five significant digits after the decimal point. Notice also the semicolon after the scale statement. Semicolons can be used in place of a new line to separate multiple statements on the same line. The <TT>sqrt</TT> function of <TT>bc</TT> takes a number in parantheses, and returns the square root of that number. Since the shell expands the variable to the value it contains, <TT>bc</TT> actually receives the number contained in the variable rather than the name of the variable itself. Finally, at the end of line 5, the output is piped to the <TT>bc</TT> command itself. The <TT>?l</TT> option tells <TT>bc</TT> to preload the math function library, which contains the <TT>sqrt</TT> function.


This article was imported from zZine. (original author: ismail)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.01849 seconds


About Us | Privacy Policy | Mission Statement | Help