语法

for 变量名 in 列表
do
    程序段(command)
done
#显示1到10的数值,自动补全,01,02,03,04
for i in $(seq 1 10)
do
    echo $i
done

#显示1到100的数值
for i in {1..100}
do
    echo $i
done

#字符循环
for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do
    echo "$day"
done
   

实现批量创建用户

#!/usr/bin/bash
#(while :) 等价于 (while true)
while :
do
    read -p "please enter prefix &  passwd & num: " prefix passwd num
    printf "user infomation:
    ----------------------
    user prefix: $prefix
    user passwd: $passwd
    user num: $num
    ----------------------
    "

    read -p "Are you sure? [y/n]: " action
    if [ $action = "y" ];then
        break
    fi
done

for i in `seq -w $num`

do
    user=$prefix$i
    id $user &>/dev/null
    if [ $? -eq 0 ];then
        echo "user $user already exists"
    else
        useradd $user
        echo "$passwd" | passwd --stdin $user &>/dev/null
        if [ $? -eq 0 ];then
            echo "$user is created"
        fi
    fi
done