For loop
Usage:
forindo 循环体done
So how do you build the list?
{1..100}
This will generate the list.
===========================================
Cases:
#!/bin/bash#forin {1..100};do echo$Idone
Seq
You can also generate lists with the SEQ command
Usage:
seq [起始数 [步进长度]] 结束数
Cases:
[Root@iZ28g26851kZ~]# seq 1123456789Ten[Root@iZ28g26851kZ~]# seq 1 213579[Root@iZ28g26851kZ~]# seq123456789Ten[Root@iZ28g26851kZ~]#
The application of SEQ in the For loop
#!/bin/bash#forin1210`;do echo$Idone
Remember to use anti-single quotes to cause yo ~
Example: for 1 to 100 and
#!/bin/bash#sum=0forin100`;do let sum=$sum+$Idoneecho"sum=${sum}"
Declare
As I've said before, in bash, variables are all character types by default.
How can a variable be declared as an integral type directly?
Declare can declare variables directly as integral type
#!/bin/bash#declare -i sum=0forin100`;do let sum=$sum+$Idoneecho"sum=${sum}"
Example: Say hello to every user in the system
#!/bin/bash#forin-d-f1`;do echo"hello ${NAME}~"done
Example: Say hello to every user in the system and show their shell~
#!/bin/bash#forin-d-f1`;do echo"hello ${NAME}~...........`cat /etc/passwd | grep ^$NAME | cut -d: -f7`"done
Example: Only the user question mark for the shell in the system is bash,
#!/bin/bash# forNAMEinch' CAT/ETC/PASSWD | Cut- D:- F1 '; DoShell_name= ' cat/etc/passwd | grep ^$NAME| Cut- D:- F7 | Sed-r' [Email protected]^/.*/(. *) @\[email protected] '`if[$SHELL _name=="Bash"]; Then Echo "Hello ${name}~ ..... ' CAT/ETC/PASSWD | grep ^$NAME | cut-d:-f7 ' " fi Done
Linux-based shell programming (4)-for loop