For loop:
loop operation, for Loop statement
1 . Command format:
for variable in list;
Loop body
Done
2 . Command function:
Execute Loop statement
3 . Command parameters:
for variable in list;
Loop body
Done
The list generation method for the For loop:
{N.. m}: Through curly braces, the middle is the variable traversal range, from N to m are variable traversal
' seq [ start number [step length]] end number ' SEQ 1 2 10 ' variable with a traversal value of 1 3 5 7 9 over 10 exit traversal
{1..100}
Declare-i sum=0 Declare declaring variable-I declares a variable to be an integral integer
Declare-x declaring variables as environment variables
4 . Command instance:
1, calculated from 1 to 100 and
#!/bin/bash
# program:
# calculates 1 to 100 of the and
# History Donggen 2016-10-21-20:30
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
Export PATH
Declare-i sum=0
For I in {1..100}; Do #或者 for I in ' SEQ 1 100 '
Let sum= $SUM + $I
Done
echo "1+. +100= $SUM "
2. write a script:
1 , the value of the set variable file is/etc/passwd
2 , say hello to each user in/etc/passwd, and show each other's shell as follows:
Hello, Root, your Shell:/bin/bash
#!/bin/bash
#program:
# Say hello to the users inside/etc/passwd
#history 2016-10-21-21:30
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash
Export PATH
lines= ' Wc-l/etc/passwd | Cut-d '-f1 '
For I in ' seq 1 $LINES '; Do
echo "Helo, ' head-n $I/etc/passwd | Tail-1 | cut-d:-f1 ' Your shell is: ' Head-n $I/etc/passwd | Tail-1 | cut-d:-f7 ' "
Done
This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1864425
Linux command: For loop