1. for循环

  • 打印1-10的和 ```bash

    bin/bash

sum=0 for ((i=1;i<=10;i++)) do sum=$[$sum+$i] done echo $sum

  1. <a name="cuTGC"></a>
  2. # 2. while循环
  3. <a name="n9XLa"></a>
  4. ## 100以内猜数字
  5. ```bash
  6. #bin/bash
  7. NUM=$[$RANDOM%100]
  8. #echo $NUM
  9. GS=101
  10. #while true
  11. while [ $GS -ne $NUM]
  12. do
  13. read -p "输入你猜的数字" GS
  14. if [ $GS -lt $NUM ];then
  15. echo "你猜的数字太小了"
  16. elif [ $GS -gt $NUM ];then
  17. echo "你猜的数字太大了"
  18. else
  19. echo "恭喜你,猜对了数字:$NUM"
  20. exit 6
  21. fi
  22. done

while循环读取文件的3个方法

  • while输入重定向(推荐) ```bash

    /bin/bash

while read USER do useradd $USER PASSWORD=$(openssl rand -base64 8 | cut -c1-6) echo $PASSWORD | password —stdin $USER >& /dev/null if [ $? -eq 0 ];then echo create $USER sucessfully fi echo username:$USER password:$PASSWORD >> /root/zcw/userinfo done < /root/zcw/userlist

  1. ```bash
  2. # cat iplist
  3. 192.168.147.101 5225
  4. 192.168.147.102 2234
  5. 192.168.147.103 4922
  6. # cat while-read.sh
  7. #!/bin/sh
  8. while read line
  9. do
  10. IP=$(echo $line |awk '{print $1}')
  11. PORT=$(echo $line |awk '{print $2}')
  12. echo "IP: $IP, PORT: $PORT"
  13. done <iplist
  14. [root@zhouchunwei-6-60 zcw]# ./while-read.sh
  15. IP: 192.168.147.101, PORT: 5225
  16. IP: 192.168.147.102, PORT: 2234
  17. IP: 192.168.147.103, PORT: 4922
  • 采用exc读取文件,然后进入while循环处理

    exec < /root/zcw/userlist while read USER
    do
      useradd $USER
      PASSWORD=$(openssl rand -base64 8 | cut -c1-6)
    echo $PASSWORD | password --stdin $USER >& /dev/null
    if [ $? -eq 0 ];then
        echo create $USER sucessfully
    fi
    echo username:$USER password:$PASSWORD >> /root/zcw/userinfo
    done
    
  • 使用cat读文件,然后通过管道进入while循环处理

    cat /root/zcw/userlist | while read USER
    do
      useradd $USER
      PASSWORD=$(openssl rand -base64 8 | cut -c1-6)
    echo $PASSWORD | password --stdin $USER >& /dev/null
    if [ $? -eq 0 ];then
        echo create $USER sucessfully
    fi
    echo username:$USER password:$PASSWORD >> /root/zcw/userinfo
    done
    

    3. until循环

    与while循环相反

    100以内猜数字

    ```bash

    /bin/bash

NUM=$[$RANDOM%100] GS=101 echo “显示结果:$NUM”

until [ $GS -eq $NUM ] do read -p “Input your number: “ GS if [ $GS -lt $NUM ];then echo “你猜的数字太小了” elif [ $GS -gt $NUM ];then echo “你猜的数字太大了” else echo “恭喜你,猜对了数字:$NUM” exit 6 fi done

```bash
#/bin/bash

NUM=$[$RANDOM%100]
GS=101
echo "显示结果:$NUM"

until [ $GS -eq $NUM ]
do
        read -p "Input your number:  " GS
        if [ $GS -lt $NUM ];then
                echo "你猜的数字太小了"
        elif [ $GS -gt $NUM ];then
                echo "你猜的数字太大了"
        elif [ $GS -eq $NUM ];then
                echo "恭喜你,猜对了数字:$NUM"
                exit 6
    else
        echo "Input ERROR"
        fi
done

4. continue && break

continue

用于跳出当前循环

打印乘法表

结果只打印5列

for ((x=1;x<10;x++))
do
  for ((y=1;y<=x;y++))
  do
    if [ $y -eq 5 ];then
      continue
    fi
    echo -ne "$y * $x = $[$x*$y]\t"
  done
  echo ""
done

break

跳出整个循环

for ((x=1;x<10;x++))
do
  for ((y=1;y<=x;y++))
  do
    if [ $y -gt 5 ];then
      break
    fi
    echo -ne "$x * $y = $[$x*$y]\t"
  done
  echo ""
done