Various examples of for, while and case statements (including commonly used escape characters, the most important thing is to type and practice more!)

Various examples of for, while and case statements (more typing, more practice, so easy!)

  • while statement format
while [ condition ] <==中括号内的状态就是判断式
do        <==是循环的开始!
    程序段落
done        <==done是循环的结束
  • for statement format
for 变量名 in 取值列表 
do
   程序段 
done

1. The sum of integers from 1 to 100

1. for statement

[root@localhost ~]#vim sum.sh
[root@localhost ~]#./sum.sh 
5050
[root@localhost ~]#cat sum.sh
#!/bin/bash
sum=0
for i in {1..100}
do
sum=$[sum+i]
done
echo $sum

2.while statement

[root@localhost ~]#./sum1.sh
运算结果是:5050
[root@localhost ~]#cat sum1.sh
#!/bin/bash
sum=0
i=0
while [ ${i} -ne 100 ]
do
i=$[$i+1]
sum=$[$sum+$i]
done
echo "运算结果是:$sum"

2. Prompt the user to enter an integer less than 100, and calculate the sum of all integers from 1 to this number

1. for statement

[root@localhost ~]#cat cal.sh 
#!/bin/bash
read -p "输入整数值(1-100):" numb
sum=0
for (( i=1; i<=${numb}; i++ ))
do
  sum=$[sum+i]
done
echo "从1到输入的值的整数值的整数和为:$sum"
[root@localhost ~]#./cal.sh 
输入整数值(1-100):21
从1到输入的值的整数值的整数和为:231

2.while statement

[root@localhost ~]#cat cal1.sh 
#!/bin/bash
read -p "请输入整数值(1-100):" nu
sum=0
i=0
while [ ${i} -ne ${nu} ]
do
i=$[$i+1]
sum=$[sum+i]
done
echo "从1到${nu}的运算结果是:$sum"
[root@localhost ~]#./cal1.sh 
请输入整数值(1-100):99
从1到99的运算结果是:4950

Three, find the even and odd sums of all integers from 1 to 100

1. for statement

[root@localhost ~]#vim sum2.sh
[root@localhost ~]#chmod +x sum2.sh 
[root@localhost ~]#./sum2.sh
所有偶数和为:2550
所有奇数和为:2500
[root@localhost ~]#cat sum2.sh 
#!/bin/bash
sum1=0
sum2=0
for ((i=0;i<=100;i+=2))
do
        sum1=$[$sum1+$i]
done
    echo "所有偶数和为:$sum1"
for ((i=1;i<=100;i+=2))
do
        sum2=$[$sum2+$i]
done
    echo "所有奇数和为:$sum2"

2.while statement

[root@localhost ~]#cat sum3.sh
#!/bin/bash
sum1=0
sum2=0
i=1
q=0
while [ $i -le 100 ]
do
sum1=$[$sum1+$i]
let i=$[$i+2]
done
echo "奇数和为:$sum1"

while [ $q -le 100 ]
do
sum2=$[$sum2+$q]
let q=$[$q+2]
done
echo "偶数和为:$sum2"

[root@localhost ~]#chmod +x sum3.sh 
[root@localhost ~]#./sum3.sh 
奇数和为:2500
偶数和为:2550

Four, shopping scripts

It is required to write a script for shopping on Taobao to buy goods. Each store has five kinds of goods to buy (500 yuan for clothes, 400 yuan for pants, 350 yuan for shoes, 150 yuan for hats, and 50 yuan for socks). Prompt the user whether to continue to the next store, and if not to continue shopping, the shopping cart will be used to settle the total amount.

[root@localhost ~]#vim shopping.sh
[root@localhost ~]#chmod +x shopping.sh 
[root@localhost ~]#cat shopping.sh 
#!/bin/bash
i=1
sum=0
while true
do
 echo "来到第$i家商店门口"
 read -p "是否进入看看(yes/no)" doing
 while [ $doing = "yes" ]
 do
  echo "1:衣服¥500"
  echo "2:裤子¥400"
  echo "3:鞋子¥350"
  echo "4:帽子¥150"
  echo "5:袜子¥50"
  echo "其他:放弃购买"
  read -p "请选择需要购买的商品序列:" num
  case $num in
  1)
   echo "衣服购买成功"
   sum=$[sum+=500]
   ;;
  2)
   echo "裤子购买成功"
   sum=$[sum+=400]
   ;;
  3)
   echo "鞋子购买成功"
   sum=$[sum+=350]
   ;;
  4)
   echo "帽子购买成功"
   sum=$[sum+=150]
   ;;
  5)
   echo "袜子购买成功"
   sum=$[sum+=50]
   ;; 
  *)
   echo "放弃购买"
            
  esac
  read -p "是否继续在这家店购物(yes/no)" doing      
 done 

 read -p "是否继续逛街(yes/no)" going
 if [ $going = "yes" ]
 then
  let i++
 else
  break
 fi 
done
echo -e "一共经过$i家店。\n合计购物总价:$sum。"
[root@localhost ~]#./shopping.sh 
来到第1家商店门口
是否进入看看(yes/no)yes
1:衣服¥500
2:裤子¥400
3:鞋子¥350
4:帽子¥150
5:袜子¥50
其他:放弃购买
请选择需要购买的商品序列:1
衣服购买成功
是否继续在这家店购物(yes/no)yes
1:衣服¥500
2:裤子¥400
3:鞋子¥350
4:帽子¥150
5:袜子¥50
其他:放弃购买
请选择需要购买的商品序列:4
帽子购买成功
是否继续在这家店购物(yes/no)no
是否继续逛街(yes/no)yes
来到第2家商店门口
是否进入看看(yes/no)yes
1:衣服¥500
2:裤子¥400
3:鞋子¥350
4:帽子¥150
5:袜子¥50
其他:放弃购买
请选择需要购买的商品序列:5
袜子购买成功
是否继续在这家店购物(yes/no)no
是否继续逛街(yes/no)no
一共经过2家店。
合计购物总价:700。

Five, batch ping hosts

Use the for statement to perform real-time detection on the network segment from 192.168.2.1 to 192.168.2.100.

[root@localhost ~]#vim pingipadd.sh
[root@localhost ~]#cat pingipadd.sh
#!/bin/bash
network="192.168.2" #先定义一个网段的网络部分,主机部分后续分配
for ip in $(seq 1 100)
#seq表示连续的数。这里可以换成{1..100}
do
  ping -c 2 -W 1 ${network}.${ip} &> /dev/null && result=0 || result=1

ping通了显示online,否则off

  if [ "${result}" -eq 0 ] 
  then
    echo "主机${network}.${ip} online." 
  else
    echo "主机${network}.${ip} off."
  fi
done

[root@localhost ~]#./pingipadd.sh 
主机192.168.2.1 online.
主机192.168.2.2 online.
主机192.168.2.3 online.
主机192.168.2.4 online.
主机192.168.2.5 off.
主机192.168.2.6 off.
主机192.168.2.7 off.
主机192.168.2.8 off.
主机192.168.2.9 off.
主机192.168.2.10 off.
^Z
[3]+  已停止               ./pingipadd.sh

Six, batch add users

The user name is placed in a users.txt file, one per line, which can be obtained by cat

The initial password is set to 123456

[root@localhost ~]#vim users.sh
[root@localhost ~]#vim users.txt 
[root@localhost ~]#cat users.sh
#!/bin/bash
urs=`cat users.txt`
for i in $urs
do
  useradd $i
  echo "123456" | passwd --stdin $i
done
[root@localhost ~]#cat users.txt 
ww1
ww2
ww3
ww4
[root@localhost ~]#chmod +x users.sh
[root@localhost ~]#./users.sh 
useradd:用户“ww1”已存在
更改用户 ww1 的密码 。
passwd:所有的身份验证令牌已经成功更新。
useradd:用户“ww2”已存在
更改用户 ww2 的密码 。
passwd:所有的身份验证令牌已经成功更新。
useradd:用户“ww3”已存在
更改用户 ww3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
useradd:用户“ww4”已存在
更改用户 ww4 的密码 。
passwd:所有的身份验证令牌已经成功更新。

To verify whether the addition is successful, use cat /etc/passwd to view

[root@localhost ~]#cat /etc/passwd
......
ww1:x:1240:1240::/home/ww1:/bin/bash
ww2:x:1241:1241::/home/ww2:/bin/bash
ww3:x:1242:1242::/home/ww3:/bin/bash
ww4:x:1243:1243::/home/ww4:/bin/bash

Seven, commonly used escape characters

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"前面的字符不会被覆盖

\\ 表示插入"\"本身

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/111873633