shell循环(国外英文资料).doc
文本预览下载声明
shell循环(国外英文资料)
* * * * * * * * *
While statement
* * * * * * * * *
General format:
While the test conditions
The do
Command table
The done
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Example exam13. Sh
#! / bin/sh
While [$1]
The do
If [-f $1]
Then echo display: $1
The cat $1
The else echo $1 is not a file name.
fi
The shift
The done
Right? Program function
Right? How to test, add parameters
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Example exam14. Sh
#! / bin/sh
Num = 1
While [$num - le 10]
The do
SUM = ` expr $num \ * $num `
Echo $SUM
Num = ` expr ` $num + 1
The done
Echo num = $num
Right? Program function 1-10
Right? How to test, run directly
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Example exam15. Sh
#! / bin/sh
Sum = 0
I = 0
While true
The do
I = ` expr $I + 1 `
Sum = ` expr $sum + $I `
If [$i-eq 100]
then
break
fi
The done
Echo Sum is: $Sum
Echo I is: $I
Right? Program function: 1-100 cumulative
Right? How to test, run directly
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Example exam16
#! / bin/sh
I = 10
While [$i-gt 5]
The do
Echo $I
(I --)
The done
Echo $I
For example, there are two things in the world
* * * * * * *
For statement
* * * * * * *
For example, this is the case for the problem
Format 1:
For variable in seq string
The do
The action
The done
Example exam17. Sh
#! / bin/sh
For I in $(seq 10)
The do
Echo $I
The done
Seq is the natural number seq 2, 10; 20 10 seq
For example, this is the case for the problem
Format 2:
((assignment; condition; operation statement))
The do
The action
The done.
Example exam18. Sh
#! / bin/sh
(I = 1; I = 10; I + +)); The do
Echo $I;
The done.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
显示全部