Shell loop statements (for loop; while loop; until loop) and interrupt (break and continue)

for loop statement

  • Read different variable values ​​to execute the same group of commands one by one
for 变量名 in 取值列表
do                   
   命令序列
done

Traverse

for i in {1..10}
      或 $(seq 1 10)
      或 ((i=1; i<=10; i++))
do
echo $i
done 

Insert picture description here

Insert picture description here

for i in {1..10..2} 
      或 $(seq 1 2 10)
      或 ((i=1; i<=10; i++))
do
echo $i
done 

Insert picture description here
Insert picture description here

Example 1: Add users in batch

①Create a user name file
Insert picture description here
②Write a script

#!/bin/bash

a=$(cat name.txt)
 for i in a
do
 useradd $i
 echo "123456" | passwd --stdin $i
done

Insert picture description here
③Verification
Insert picture description here

Example 2: Check the status of the host based on the IP address

#!/bin/bash

for i in 192.168.100.{1..20}
do
  ping -c 3 -i 0.5 -W 2 $i &> /dev/null
if [ $? = 0 ]
 then
  echo "$i online"
 else
  echo "$i offline"
fi
done

Insert picture description here
Insert picture description here

while loop statement

  • Test a certain condition repeatedly, and execute it repeatedly as long as the condition is true
while 条件测试操作
do
   命令序列
done
#!/bin/bash

i=0
while (($i <=10))
do
echo "$i"
let i++
done

Insert picture description here
Insert picture description here

Example 1 Guess the price game

#!/bin/bash

price=$[$RANDOM % 1000]
a=0
times=0
echo "猜猜商品价格是多少"

while [ $a -eq 0 ]
do
let times++

read -p "请输入你猜的价格:" b
if [ $b -eq $price ];then
  echo "yes!"
  let a++

elif [ $b -gt $price ];then
  echo "你猜大了!"
elif [ $b -lt $price ];then
  echo "你猜小了!"
fi

done
echo "你总共猜了 $times 次"

Insert picture description here
Insert picture description here

Example 2: Add users in batches

#!/bin/bash

i=0
while [ $i -le 4 ]
 do
 let i++
useradd stu$i
 echo "123456" | passwd --stdin stu$i
done

Insert picture description here
Insert picture description here

until loop statement

  • Test a certain condition repeatedly, and execute it repeatedly as long as the condition is not established
until 条件测试操作
do
   命令序列
done
#显示1-10的整数
#!/bin/bash

i=1
until [ $i -gt 10 ]
do
 echo "$i"
 let i++
done

Insert picture description here
Insert picture description here

Example: Calculate the value from 1 to 50

#!/bin/bash

i=1
sum=0
until [ $i -gt 50 ]
do
 sum=$(($sum+$i))
 let i++
done
 echo "$sum"

Insert picture description here
Insert picture description here

Break (break and continue)

①break

  • break out of a single loop
#!/bin/bash

for i in {1..5}
do
echo "外层循环 $i"
 for b in {1..5}
 do
 if [ $b -eq 3 ]
  then
  break
 fi
  echo "内层循环 $b"
done
done

Insert picture description here
Insert picture description here

②continue

  • continue to abort a command in a loop, but not completely abort the entire command
#!/bin/bash

for i in {1..5}
 do
  echo "外层循环 $i"
  for b in {1..5}
 do
 if [ $b -eq 3 ]
  then
  continue
 fi
  echo "内层循环 $b"
 done
done

Insert picture description here
Insert picture description here

IFS field separator

  • Contains spaces, tabs, and newlines by default
1.修改
IFS=$'\t\n'

修改成只换行
IFS=$'\n'
IFS=':'
IFS=','

2.备份
IFS. OLD=$IFS
IFS=$'\n'
...
IFS=$IFS.OLD

Example: output all directories contained in the environment variable PATH and all executable files in them

#!/bin/bash

OLDIFS=$IFS
IFS=':'
  for i in $PATH
 do
  for a in $i/*
 do
 if [ -x $a -a -f $a ];then
  echo "$a 文件有执行权限"
 fi
 done
done

IFS=$OLDIFS

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_53496478/article/details/114651528