Shell Programming-case Statement and Loop Statement

case multi-branch statement

The Structure of case Statement
 Different command sequences are executed for different values of variables

Example 1

 Keyboard Type Recognition
   Prompt the user to enter a character
   Determine that the character is an alphabet, number, or other character
#!/bin/bash
read -p "Please enter a character:" key
case $key in
[a-z] | [A-Z])
    echo "You enter letters."
     ;;
[0-9])
    echo "What you enter is a number."
    ;;
*)
    echo "You enter special characters."
esac

Example 2

Enter grades and check grades

#!/bin/bash
read -p "Please enter your score:" num
case $num in
[8-9][0-9]|100)
    echo "excellent"
    ;;
7[0-9])
    echo "good"
    ;;
6[0-9])
    echo "qualified"
    ;;
[0-9]|[1-5][0-9])
    echo "Unqualified"
    ;;
*)
    echo "You entered incorrectly"
esac

Structure of for statement

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

Example 1

 Batch Adding Users
 Store the username in the name.txt file, one for each line
 Initial passwords are set to 123456
 Verification script

#!/bin/bash
TMP=$(cat /root/name.txt)
for USER in $TMP
do
        echo "Users are $USER"
        useradd $USER 
        echo "123456" | passwd --stdin $USER > /dev/null
done

Example 2

 Check host status according to IP geology
 IP geology is stored in demo04.txt file, one per line
 Using ping command to detect the connectivity of each host

#!/bin/bash
IP=$(cat /root/demo04.txt)
for ip in $IP
do
  ping -c 1  -s 1 -w 3 $ip > /dev/null
 if [ $? -eq 0 ]
 then
   echo "$ip Host exists"
 else
   echo "$ip Host does not exist"
 fi
done

Structure of while statement

Test a condition repeatedly, and execute it repeatedly as long as the condition holds.

Example 1

 Batch Adding Users
 User names start with stu and are numbered numerically
 A total of 5 users were added, namely stu1,stu2,stu3...
 Initial passwords are set to 123456
#!/bin/bash
num=1
while [ $num -le 5 ]
do
useradd stu$num
  echo "123456" | passwd --stdin stu$num &> /dev/null
  let num++
done

Example 2

 Guess the commodity price script
 Obtaining RANDOM Numbers by RANDOM Variable
 Prompt the user to guess and record the number of times, guess and exit the loop
#!/bin/bash
random=$(expr $RANDOM % 100)
tim=0
while true
do
 read -p "Please enter the price of the goods:" jia
 let tim++
 if [ $jia -eq $random ]
 then
  echo "Congratulations on your guess"
  echo "You guessed it altogether. $tim"
 exit 0
 elif [ $jia -lt $random ]
 then
  echo "Your guess is small."
 else
  echo "You guessed a lot."
 fi
done

Comprehensive examples

Go into five stores and buy goods separately. Final check the total consumption.

#!/bin/bash
i=1
sum=0
while [ $i -le 5 ]
do
  echo "Enter $i Home Store"
  read -p "Do you want to go in and have a look?(yes/no)" doing
  while [ $doing = "yes" ]
    do
       echo "1:clothes¥200"
       echo "2:shoes¥100"
       echo "3:glove¥75"
       echo "4:trousers¥150"
       read -p "Please select the order of goods to be purchased:" num
       case $num in
           1)
             echo "Successful clothing purchase"
             expr $[sum+=200] &> /dev/null
           ;;
           2)
             echo "Successful Shoe Purchase"
             expr $[sum+=100] &> /dev/null
           ;;
           3)
             echo "Successful Glove Purchase"
             expr $[sum+=75] &> /dev/null
           ;;
           *)
             echo "Successful pants purchase"
             expr $[sum+=150 &> /dev/null
      esac
      read -p "Whether to Continue Purchasing(yes/no)" doing

   done
   let i++
   if [ $doing = "no" ]
     then
     continue
   fi
done
   echo "Total Shopping Price:$sum"

Structure of until statement

Repeated testing of a condition, as long as the condition is not established, repeated execution

Example 1

 Calculate the sum of 1-50
 Calculate the sum of 1-50 by cyclic accumulation
#!/bin/bash
sum=0
i=0
until [ $i -eq 51 ]
do
  let sum+=$i
  let i++
done
echo "The sum of the total is: $sum"

Example 2

 Send online messages to specified users
 If the user is not online (not logged in to the system), try every 5 seconds until the user logs in and sends a message.
 Users and messages are passed to the script through parameters so far
#!/bin/bash
if [ $# -lt 0 ]
then
  echo "Usage:$0 <user><message>"
  exit 1
fi
grep "$1" /etc/passwd &> /dev/null
if [ $? -eq 0 ];then :
else
  echo "user does not exist"
fi
until who|grep "$1" > /dev/null
do
 echo "Users are not online"
 sleep 5
done
echo $2 | write $1


Thank you for reading!!!

Tags: Linux

Posted on Wed, 09 Oct 2019 06:40:23 -0700 by faizulbari