for语法一:
for 变量 in 值1 值2 值3 (可以是一个文件等)
do
程序
done
注:这种语法中for循环的次数,取决于in后面值的个数(空格分割),有几个值就循环几次,并且每次循环都把值赋予变量。
for语法二:
for ((初始值;循环控制条件;变量变化))
do
程序
done
注:语法二注意:
初始值:在循环开始时,需要给某个变量赋予初始值,如i=1
循环控制条件:用于指定变量循环次数,如i<=100,则只要i的值小于等于100,循环就会继续
变量变化:每次循环之后,变量该如何变化,如i=i+1。代表每次循环之后,变量i的值都加1
案例1:
for循环,值为普通字符串
#!/bin/bash for F in file1 file2 file3 file4 do echo the text is $F done for I in {1..100} do touch hehe$I done
案例2:
for取值比较复杂,使用引号或转义符
#!/bin/bash for F in file1 "hello beijing" file4 "hello world" do echo the text is $F done for I in file1 file\'2 I\'S do echo the text is $I done
案例3:
for循环,从变量中取值
#!/bin/bash List="file1 file2 file3" for F in $List do echo the text is $F done
案例4:
for循环,从命令中取值;默认空格为分隔符,可以使用IFS自定义
#!/bin/bash for H in $(cat /etc/hosts) do echo "$H" done #以冒号做分隔符 IFS=: #以冒号、分号、双引号做分隔符 IFS=:;" #以换行符做字段分隔符 IFS=$'\n'
案例5:
for循环自增自减,c语言风格
循环递增加1
#!/bin/bash for (( i=1;i<=100;i++ )) do useradd sc$i done
同时输出1-9的升序和降序
#!/bin/bash for (( a=1,b=9;a<10;a++,b-- )) do echo num is $a $b done
#!/bin/bash a=0 b=10 for i in {1..9} do let a++ let b-- echo num is $a $b done
案例6:
批量创建用户,提示用户输入前缀、数量;要求当前用户为root,前缀不为空,密码随机
#!/bin/bash if [ ! $UID -eq 0 ] && [ $USER == "root" ];then echo "当前不是管理员,无权执行" exit fi read -p "请输入你的用户前缀:" User_qz if [ -z $User_qz ];then echo "请输入有效的值" exit fi read -p "请输入你的用户数量:" User_num if [[ ! $User_num =~ ^[0-9]+$ ]];then echo "请输入整数" exit fi echo "你要创建用户 ${User_qz}1 ~ ${User_qz}${User_num}" read -p "你要创建用户如下,请确认【y|n】 " yn case $yn in y) for i in $(seq $User_num) do user=${User_qz}$i id $user &> /dev/null if [ $? -eq 0 ];then echo "useradd:user $user already exists" else user_pass=$(echo $((RANDOM))|md5sum|cut -c 2-10) useradd $user echo "$user_pass" | passwd --stdin $user &> /dev/null echo "用户: $user 密码:$user_pass" >> /tmp/user.txt echo "useradd: user $user add successfull,密码在/tmp/user.txt" fi done ;; n) exit ;; *) echo "USAGE: 请输入【y|n】" esac #(随机数的公式: RANDOM % n + 1 得到 1-n范围的随机数)
案例7:
批量探测主机是否存活(192.168.8.0/24),并查看存活主机22端口是否开启
#!/bin/bash for i in {1..20} do Ip=192.168.8.$i ping -c 1 -w 1 $Ip &> /dev/null if [ $? -eq 0 ];then echo "$Ip is ok" echo "$Ip" >> /tmp/ip.log else echo "$Ip is down" fi done wait echo "scan host ip is done" echo "scan ssh port is starting" for i in $(cat /tmp/ip.log) do nmap $i |grep "22" &> /dev/null if [ $? -eq 0 ];then echo "$i 22端口已开启" echo "$i 22端口已开启" >> /tmp/port.log fi done
案例8:
随机点名
创建点名册
cat >> student.txt<<EOF
张三
李四
王五
马六
朱七
EOF
#!/bin/bash num=$(wc -l student.txt|awk '{print $1}') for i in $(seq $num) do stu_num=$(( RANDOM % $num + 1 )) sed -n "${stu_num}p" student.txt sleep 0.1 done name_stu=$(sed -n "${stu_num}p" student.txt) echo -e "就是你了:\033[32m $name_stu \033[0m"
案例9:
随机猜字数
思路:
随机1-100数字 echo $(( RANDOM % 100 + 1 ))
要求输入的必须是数字
统计猜的次数
1.num=$(echo $(( RANDOM % 100 + 1 ))) i=0 while true do read -p "请输入一个随机数字【1-100】:" sz if [[ ! $sz =~ ^[0-9]+$ ]];then echo "请输入有效的整数" continue fi if [ $sz -gt $num ];then echo "你的数字过大" elif [ $sz -lt $num ];then echo "你的数字过小" else echo "恭喜,你猜对了!!!" break fi let i++ done echo "你总共猜了 $(( $i + 1 )) 次。"