Shell Programming Loop Statement

This article is mainly about the use of shell script loop statements.

for loop

Designated number of times

#!/bin/bash
for ((i=1;i<=10;i++))
do
  echo $i
done
[root@localhost ~]# vim num.sh
[root@localhost ~]# chmod +x num.sh 
[root@localhost ~]# ./num.sh 
1
2
3
4
5
6
7
8
9
10

Ergodic cycle

#!/bin/bash
TMP=$(cat username.txt)
for USER in $TMP
do
  echo "Users: $USER"
done
[root@localhost ~]# vim users.sh
[root@localhost ~]# chmod +x users.sh 
[root@localhost ~]# vim users.txt
[root@localhost ~]# cat users.txt 
zhangsan
lisi
wangwu
[root@localhost ~]# ./users.sh 
//User: zhangsan
//User: lisi
//User: wangwu
  • Adding users
#!/bin/bash
TMP=$(cat users.txt)
for USER in $TMP
do
  useradd $USER && echo "123456" | passwd --stdin $USER &> /dev/null
done
echo "Additional user information is:"
tail -$(cat users.txt | wc -l) /etc/passwd
for USER in $TMP
do
  userdel -r $USER
done
echo "Deleted added users"
[root@localhost ~]# vim useradd.sh
[root@localhost ~]# chmod +x useradd.sh 
[root@localhost ~]# ./useradd.sh 
//Additional user information is:
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wangwu:x:1003:1003::/home/wangwu:/bin/bash
//Deleted added users
  • Check host status
#!/bin/bash
TMP=$(cat ipadds.txt)
for IP in $TMP
do
  ping -c 4 -i 0.2 -W 3 $IP &> /dev/null
  if [ $? -eq 0 ]
  then
    echo "Host $IP is up"
  else
    echo "Host $IP is down"
  fi
done
[root@localhost ~]# vim ping.sh
[root@localhost ~]# chmod +x ping.sh 
[root@localhost ~]# vim ipadds.txt
[root@localhost ~]# cat ipadds.txt 
127.0.0.1
192.168.28.129
[root@localhost ~]# ./ping.sh 
Host 127.0.0.1 is up
Host 192.168.28.129 is down

while loop

  • Output 1 to 10
#!/bin/bash
num=1
while [ $num -le 10 ]
do
  echo $num
  #num=`expr $num + 1`
  let num++
done
  • Adding users
#!/bin/bash
# useradd
num=1
while [ $num -le 10 ]
do
  useradd stu$num && echo "123456" | passwd --stdin stu$num &> /dev/null
  let num++
done
echo "Additional user information is:"
tail -10 /etc/passwd
# userdel
num=1
while [ $num -le 10 ]
do
  userdel -r stu$num
  let num++
done
echo "Deleted added users"
[root@localhost ~]# vim useradd10.sh
[root@localhost ~]# ./useradd10.sh 
//Additional user information is:
stu1:x:1001:1001::/home/stu1:/bin/bash
stu2:x:1002:1002::/home/stu2:/bin/bash
stu3:x:1003:1003::/home/stu3:/bin/bash
stu4:x:1004:1004::/home/stu4:/bin/bash
stu5:x:1005:1005::/home/stu5:/bin/bash
stu6:x:1006:1006::/home/stu6:/bin/bash
stu7:x:1007:1007::/home/stu7:/bin/bash
stu8:x:1008:1008::/home/stu8:/bin/bash
stu9:x:1009:1009::/home/stu9:/bin/bash
stu10:x:1010:1010::/home/stu10:/bin/bash
//Deleted added users
  • Number guessing game

RANDOM ranges from 0 to 32767

#!/bin/bash
key=$(expr $RANDOM % 1000)
times=0
while true
do
  read -p "please enter a number(0-999): " num
  let times++
  if [ $num -eq $key ]
  then
    echo "Guess right! You guessed it. $times Secondly."
    exit 0
  elif [ $num -gt $key ]
  then
    echo "Guess high"
  else
    echo "Guess low"
  fi
done
[root@localhost ~]# vim guess.sh
[root@localhost ~]# chmod +x guess.sh 
[root@localhost ~]# ./guess.sh 
Please enter the number (0-999):500
 Guess low
 Please enter the number (0-999):750
 Guess low
 Please enter the number (0-999):875
 Guess high
 Please enter the number (0-999):800
 Guess low
 Please enter the number (0-999):850
 Guess high
 Please enter the number (0-999):825
 Guess low
 Please enter the number (0-999):835
 Guess low
 Please enter the number (0-999):840
 Guess high
 Please enter the number (0-999):838
 Guess right! You guessed nine times.

Comprehensive examples

  • Shopping system
#!/bin/bash
# Shopping System V1.1
c1=glove
c2=Loose coat
c3=trousers
c4=shoes
p1=40
p2=500
p3=150
p4=200
count=0
total=0
echo "Welcome! High quality and inexpensive! Welcome to buy!"
for ((i=1;i<=5;i++))
do
  read -p "First $i Home store, whether to enter or not(y/n): " ack
  if [ $ack = y ]
  then
    echo "The goods are as follows:"
    echo "1.$c1: ¥$p1"
    echo "2.$c2: ¥$p2"
    echo "3.$c3: ¥$p3"
    echo "4.$c4: ¥$p4"
    while true
    do
      read -p "Whether to buy or not(y/n): " ack
      if [ $ack = y ]
      then
        read -p "Please enter the serial number of the goods you need to buy:" num
        case $num in
        1)
          echo "You bought it. $c1"
          let count++
          total=`expr $total + $p1`
          ;;
        2)
          echo "You bought it. $c2"
          let count++
          total=`expr $total + $p2`
          ;;
        3)
          echo "You bought it. $c3"
          let count++
          total=`expr $total + $p3`
          ;;
        4)
          echo "You bought it. $c4"
          let count++
          total=`expr $total + $p4`
          ;;
        *)
          echo "Input error, purchase failure"
        esac
      elif [ $ack = n ]
      then
        echo "You're out of the first place. $i Home Shop"
        break
      else
        echo "Input error, please re-enter"
      fi
    done
  elif [ $ack = n ]
  then
    continue
  else
    echo "Input error, please re-enter"
    let i--
  fi
done
echo "Number of purchases: $count"
echo "Total shopping price: $total"
echo "Thank you for coming! Look forward to seeing you next time!"

Tags: Linux vim P4 shell

Posted on Wed, 09 Oct 2019 16:10:41 -0700 by Okami