Loop of shell scripts: repeating commands
1.for Cycle
Grammar
For variable name in variable Value list
Do
Command
Done
For based on the contents of the variable Value list, executes the command repeatedly until all the contents of the Variable Value column have been evaluated and finished.
Type of Value list: can be a specific text file, command generation list
Case LIST:
VI User.txt
Zsan
Lisi
: Wq
VI useradd_for.sh
#!/bin/bash
un=$ (Cat/root/bin/user.txt)
For I in $Un
Do
Useradd #i
Ehco 123123 |passwd--stdin $
Done
: Wq
chmod +x useradd_for.sh
./useradd_for.sh
Case: Checking for surviving hosts on the network
VI chk_net_alive_host.sh
#/bin/bash
Read-p "Your want chk Net:" NET # #输入192.168.100
For I in $ (SEQ 1 254)
Do
Ping-c 2 $NET. $i &&echo "$NET. $i are up."
Done
: Wq
Case LIST: Setting up the boot service
VI ck.list
Crond
dhcpd
: Wq
VI onboot_for.sh
#!/bin/bash
sl=$ (Cat/root/bin/ck.list)
For I in $SL
Do
/etc/init.d/$i status |grep PID
If [$?-ne 0];then
/etc/init.d/$i Restart
Fi
Chkconfig $i on
Done
: Wq
For value list extension: Sequence list, array list
Sequence list:
VI for_list.sh
#!/bin/bash
Fl={vsftpd,dhcpd,named}
For I in $FL
Do
/etc/init.d/$i Restart
Chkconfig $i on
Done
For i in {1..254}
Do
Ping-c 2 192.168.10. $i &>/dev/null
Done
: Wq
This article is from the "Lp-linux" blog, make sure to keep this source http://linuxlp.blog.51cto.com/11463376/1774124
Use of shell scripts---for loop