Syntax for the FOR loop:
1. For variable in value 1 value 2 value 3 ....
Do
Program
Done
For example: The following script will print 4 times respectively the value of morning noon afternoon evening
1 #!/bin/bash2#打印时间 3#Author mrfeng45 for-time in morning noon afternoon evening6do 7 Echo $time8 Done
For example, batch decompression:
1#!/bin/Bash2 #批量解压缩脚本3 #Author: Mrf4 5path=/opt/Lamp6 7 CD $path8 9 ls*.Tar. gz >ls. LogTen ls*.tgz >>ls. Log One forIinch$(Cat ls. Log) A Do - TarZXVF $i &>>cc. Log - Done the RM-rfls. Log
2.for Syntax Two
Format:
1 for (initial value; cyclic control condition; variable change)) 2 Do 3 procedure 4 Done
For example:
1#!/bin/Bash2 #从1加到1003 4s=05 for((i=1; i<= -; i=i+1))6 Do7s=$ (($s +$i))8 Done9 Echo "The sum of 1+2+...+100is: $s"
Example: adding Users in bulk
1#!/bin/Bash2 # Users who add a number of users in bulk3# AUTHOR:MR F (e-mail:22222222)4 5Read-p"Please input user name:"-T -name6Read-p"Please input the number of users:"-T -Num7Read-p"Please input the password of users:"-T -Pass8 if[!-Z"$name"-A! -Z"$num"-A! -Z"$pass" ]9 ThenTeny=$ (Echo$num |sed 's/[0-9]//g') One if[-Z"$y"] ; Then A for((i=1; i<= $num; i=i+1)) - Do -/usr/sbin/useradd $name $i &>/dev/NULL the Echo$pass | /usr/bin/passwd--stdin $n -Ame$i &>/dev/NULL - Done - fi + fi - Cat/etc/passwd|grepuser[1-$num] |Cut-D":"-F1>>userlist.log
Of course, I also wrote a script that uses Userlist.log for bulk deletion to make it easier for you to clean up after the experiment.
1 #!/bin/bash2 for in $ (cat ./Userlist.log)3 do 4 /usr/sbin/userdel-r $i &>>del.log5 Done 6 #cat del.log7rm -RF Del.log
Shell Learning Path: Process Control (for)