Use of until, while, case and other statements

Posted by cableuser on Mon, 03 Jun 2019 23:55:54 +0200

We have already introduced the basic use of while and other methods, so as to introduce you another way to follow the way and while, until special use.

1. until format

until condition ;do
    Circulatory body
done

Entry Conditions: If the judgement condition is false, it will enter the loop body for execution.
Exit condition: If the judgment condition is true, it will not enter the loop body for execution.

Note: This is different from while, which is easy to confuse for beginners.

Example: Find the sum of all positive integers within 100

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

Example: Print 99 multiplication table

#!/bin/bash
#
declare -i i=1
declare -i j=1
until [ -j -gt 9 ];do
    until [ $i -gt $j ];do
            echo -n -e "$i*$J=$[$i*$J]\t"
            let i++
    done
    echo
    let i=1
    let j++
done

2. Special Usage in Loop Control Statements (Used in Loop Body)
Continu [n]: End the current cycle of Layer N ahead of time and go directly to the next round of judgment

    while condition;do
        CMD1
        CMD2
        ...
        if condition2;then
            continue
        fi
        COM
     done

Brea [n]: End Layer N loops ahead of time

    while condition;do
        CMD1
        CMD2
        ...
        if condition2;then
            break
        fi
        COM
     done

Example: Find the sum of all even numbers within 100, requiring a loop to traverse all positive integers within 100

#!/bin/bash
#
declare -i i=0
declare -i sum=0

until [ $i -gt 100 ];do
    let i++
    if [ $[$i%2] -eq 1 ];then
            continue
    fi
    let sum+=$i
done
echo "Sum $sum;"

3. Create a Dead Cycle
If we want to judge a loop, when the loop meets a certain condition, we can combine the continue or break commands.

while true;do
    Circulatory body
done

until false;do  
    Circulatory body
done                 

Example: Every 3 seconds to the system to get the information of the user who has logged in, if docker logged in, it is recorded in the log, and exit;

#!/bin/bash
#
read -p "enter a user name "username
while true;do
    if who |grep "^$username" &>/dev/null;then
            break
    fi
    sleep 3
done

echo "$username is logged." >>/tmp/usr.log

The second method

#!/bin/bash
#
read -p "enter a user name "username
until  who |grep "^$username" &>/dev/null;do
    sleep 3
done

echo "$username is logged." >>/tmp/usr.log

4. Special use of while loops (traversing every line of a file)

while read line ;do
    Circulatory body
done </PATH/FROM/SOMEFILE
 Read every line in the / PATH/FROM/SOMEFILE file once and assign the line to the variable line.

Example: Find out all users whose ID number is even and display their user and ID number

#!/bin/bash
#
while read line ;do
    if [ $[`echo $line |cut -d: -f3`%2] -eq 0 ];then
            echo -e -n "userName:`echo $line |cut -d: -f1`\t"
            echo "uid:`echo $line |cut -d: -f3`"
    fi
done < /etc/passwd

5. Special formats for loops:

For ((initialization of control variables; conditional judgment expression; modified expression of control variables); do)
    Circulatory body
done

Initialization of control variables: only once when running to the loop code segment;
Control variable modification expression: At the end of each cycle, the control variable modification operation will be carried out first, and then the conditional judgment will be made.

Example: Find the sum of all positive integers within 100

declare -i sum=0
for((i=1;i<=100;i++));do
    let sum+=$i
done
echo "Sum $sum"

Example: 99 multiplication table

#!/bin/bash
#
for((j=1;j<=9;j++));do
    for((i=1;i<=j;i++));do
        echo -e -n "${i}*${j}=$[$i*$j]\t"
    done
done

Write a script to accomplish the following tasks
(1) Display a menu as follows:
cpu)show cpu information;
mem)show memory information;
disk)show disk information;
quit)quit
(2) prompting users to select items;
(3) Display the content selected by the user;
Further, it shows that the script does not exit after completion, but prompts the user to continue displaying other content, and quits only when quit is used.

#!/bin/bash
#
while true ;do
    while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a
"$option" != 'quit' ];do
    read -p  "wrong option,enter a option: " option
    done

    case "$option" in
            cpu)
                    lscpu
            ;;
            mem)
                    cat /proc/meminfo
            ;;
            disk)
                    fdisk -l
            ;;

            quit)
                    echo "quit ..."
                    break
            ;;
    esac
    read -p "Please enter option again: " option
done

5. Conditional Judgment: case Statement

case variable reference in
pat1)
    Branch 1
;;
pat2)
    Branch 2
    ;;
...
*)
    Default branch
esac

Exercise: Write a script to fulfill the following requirements
(1) script acceptable parameters: start, stop, restart, status
(2) If the parameter is not one of the four, it is prompted to exit after using the format.
(3) If it is start: create / var/lock/subsys/script_name and display "Start Successfully"
If the implementation has stopped, how to deal with it
(4) If stop: delete / var/lock/subsys/script_name and display "stop successful"
If there was no start, how to deal with it
(5) If it is status, then
If the file / var/lock/subsys/script_name exists, it shows "script_name is running..."
If / var/lock/subsys/script_name does not exist, then "script_name is stopped..."
Where: script_name is the current script name

#!/bin/bash
#

script=$0
read -p "Enter a option: " option
    echo "wrong option."
    read -p "Enter a option: " option

done

case $option in
    start)
    touch /var/lock/subsys/$script
    echo "$script is running..."
    ;;
    stop)
    rm -rf /var/lock/subsys/$script
    echo "$script is stopped..."
    ;;
    status)
            if [ -e $script ];then
                    echo "$script is running..."
            else
                    echo "$script is stopped..."
            fi
    ;;
    *)
            echo "quit..."
            ;;
esac

Topics: Docker