Shell programming loop statement (detailed explanation and script case)

Shell programming loop statement (detailed explanation and script case)

One, the structure of the for statement

Read different variable values ​​to execute the same group of commands one by one

for 变量名 in 取值列表
do
  命令序列
done
for 收件人 in 邮件地址列表
do
  发送邮件
done

Insert picture description here

{1..10}

   $(seq 1 10)          #指连续的1到10 

((i=1;i<=10;i++))

Insert picture description here

{1..10..2}

$(seq 1 2 10)         #指1、3、5、7、9,1到10之间从1开始每个加2

((i=1;i<=10;i+=2))

Insert picture description here

(1) Application examples of for statement

1. Example 1-Add users in batches

The user name is stored in the users.txt file, one per line The
initial password is set to 123456
verification script

#!/bin/bash   
chmod 777 users.txt
for user in `cat /root/users.txt
  do
     useradd $user  
     echo "用户$user 已创建"   
     echo "111111" | passwd --stdin $user


  done

Insert picture description here

Insert picture description here

Insert picture description here

2. Example 2-Use to check host status based on IP address

  • The IP address is stored in the ip.txt file, one per line
  • Use the ping command to detect the connectivity of each host
#!/bin/bash
chmod 777 ip.txt
for i in `cat /root/ip.txt`
   do
    ping -c 3  $i &> /dev/null
   if [ $? -eq 0 ];then
      echo "$i host is up"
         else
            echo  "$i host is down"
   fi

  done

Insert picture description here

Insert picture description here
Insert picture description here

3. Example 3-The for loop calculates the sum of 1-100 odd numbers

#!/bin/bash
sum=0
for ((i=1;i<=100;i+=2))

do

   let sum=$sum+$i

done
   echo $sum

Insert picture description here

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-gwwRJpMY-1608366275194)(C:\Users\Chen\Desktop\9.png)]

Second, the structure of the while statement

Test a certain condition repeatedly and execute repeatedly as long as the condition is true

while 条件测试操作
do
    命令序列
done
while 未猜中正确价格
do
    反复猜测商品价格
done

Insert picture description here

(1) Application examples of while statement

1. Example 1-while loop calculation (1-100 integer sum)

#!/bin/bash
sum=0
i=0
while [ $i -le 100 ]
  do
     let sum=$sum+$i
     let i++
  done
   echo $sum

Insert picture description here

Insert picture description here

2. Example 2-while loop calculation (1-100 integer sum)

#!/bin/bash
echo "没事来猜数玩"
a=$[$RANDOM % 1000]
b=0
c=0
while [ $b -lt  1  ]
   do
   read -p "请输入你要猜的数:  " d

    let c++
    if [ $d -lt $a ];then
     echo "你猜小了"
    
    let c++
     elif [ $d -gt $a ] ;then
    echo "你猜大了" 
    
    let c++
     elif [ $d -eq $a  ];then
     echo "恭喜你才对了,它的数是$a,你一共猜了$c次!!"
  c=2

    fi
    
      done

Insert picture description here

Third, the structure of the until statement

Test a certain condition repeatedly, and execute repeatedly as long as the condition is not established

until 条件测试操作
do
    命令序列
done

until 未超过10 
do
    数字依次递增
done

Insert picture description here

1. Example 1-while loop calculation (1-100 integer sum)

#!/bin/bash
sum=0
i=0
until  [ $i -gt 100 ]
  do
     let sum=$sum+$i
     let i++
  done
   echo $sum

Insert picture description here

Fourth, the special usage of echo

echo -n 表示不换行输出

echo -e 输出转义字符,将转义后的内容输出到屏幕上

常用的转义字符如下:
\b 转义后相当于按退格键(backspace),但前提是"\b"后面存在字符;"\b"表示删除前一个字符,"\b\b"表示删除前两个字符。

\c 不换行输出,在"\c"后面不存在字符的情况下,作用相当于 echo -n; 但是当"\c"后面仍然存在字符时,"\c"后面的字符将不会被输出。

\n 换行,被输出的字符从"\n"处开始另起一行。

\f 换行,但是换行后的新行的开头位置连接着上一行的行尾;

\v 与\f相同;

\t 转以后表示插入tab,即横向制表符;

\r 光标移至行首,但不换行,相当于使用"\r"以后的字符覆盖"\r"之前同等长度的字符;但是当"\r"后面不存在任何字符时,"\r"前面的字符不会被覆盖

\\ 表示插入"\"本身;

Example:

[root@gcc ~]#echo -e "123\b456"

12456
[root@gcc ~]#echo -e "123\c456" 

123[root@gcc ~]#^C
[root@gcc ~]#echo -e "123\n456"

 123
 456
[root@gcc ~]#echo -e "123\f456" 

123   

​   456
[root@gcc ~]#echo -e "123\v456" 

123   

​    456
 [root@gcc ~]#echo -e "123\t456"

 123 456 
[root@gcc ~]#echo -e "123\r456" 

456 
[root@gcc ~]#echo -e "123\\456" 

123\456

Guess you like

Origin blog.csdn.net/weixin_51573771/article/details/111406674