[shell articles] process control. if judgment, case statement, for loop, while loop, until loop, infinite loop, jump out of loop, break, continue,

Process control

1. if judgment

1.1 Basic syntax

if else

if [ 条件判断式 ];then
	程序
fi

or

if [ 条件判断式 ]
then
	程序
fi

if else-if else

if [ 条件判断式 ];then
	程序
elif [ 条件判断式 ];then
	程序
else
	程序
fi

Precautions:

(1) [Conditional judgment expression], there must be a space between the brackets and the conditional judgment expression.

(2) There must be a space after if or elif

1.2 Practical case

a=10
b=20
if [ $a == $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi

Output result:

a 小于 b

1.3 Extension

The if else statement is often used in conjunction with the test command as follows:

num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo '两个数字相等!'
else
    echo '两个数字不相等!'
fi

Output result:

两个数字相等!

2. case statement

2.1 Basic syntax

case $变量名 in
"值1")
	如果变量的值等于1,则执行
;;
"值2")
	如果变量的值等于2,则执行
;;
*)
	如果变量的值都无法匹配,则执行
;;
esac

Precautions:

(1) The end of the case line must be the word "in"

(2) Each pattern match must end with an English closing bracket ).

(3) Double semicolon " ;; " indicates the end of the command sequence, which is equivalent to break in java.

(4) The last "*)" represents the default mode, which is equivalent to default in java.

2.2 Practical case

Enter a number, if it is 1, output banzhang, if it is 2, output cls, if it is other, output renyao.

[root@jiangnan data]# vi if3.sh
[root@jiangnan data]# cat if3.sh 
#!/bin/bash
case $1 in
"1")
	echo "banzhang"
;;
"2")
	echo "cls"
;;
*)
	echo "renyao"
;;
esac
[root@jiangnan data]# sh if3.sh 1
banzhang
[root@jiangnan data]# 

Analogy to switch...case syntax in java

3. for loop

3.1 Syntax Format 1

for var in item1 item2 ... itemN
do
   程序
done

3.2 Syntax Format 2

for ((初始值;循环控制条件;变量变化)) 
do 
   程序
done

3.3 Practical case 1

for var in 1 2 3 4 5
do
    echo "The value is: $var"
done

Output result:

The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5

3.4 Practical case 2

from 1 to 100

[root@jiangnan data]# vi for1.sh
[root@jiangnan data]# cat for1.sh 
#!/bin/bash
s=0
for ((i=0;i<=100;i++))
do
	s=$[$s+$i]
done
echo $s
[root@jiangnan data]# sh for1.sh 
5050
[root@jiangnan data]# 

Note: We talked about the case of using for loop with parameters in the previous article, pay attention to the difference between $@ and $*, you can see my previous article https://blog.csdn.net/weixin_45842494/article/ details/123490655

4. while loop

4.1 Basic syntax

while [ 条件判断式 ]
do
	程序
done

4.2 Practical cases

from 1 to 100

[root@jiangnan data]# vi while.sh
[root@jiangnan data]# cat while.sh 
#!/bin/bash
s=0
i=1
while [ $i -le 100 ]
do
	s=$[$s+$i]
	i=$[$i+1]
done
echo $s
[root@jiangnan data]# sh while.sh 
5050
[root@jiangnan data]# 

5. until loop

The until loop executes a series of commands until the condition is true and stops.

The until loop is the exact opposite of the while loop .

5.1 Basic syntax

until [ 条件判断式 ]
do
	程序
done

5.2 Practical case

Sum from 1 to 100

[root@jiangnan data]# vi until.sh 
[root@jiangnan data]# cat until.sh 
#!/bin/bash
a=0
s=0
until [ ! $a -le 100 ]
do
	s=$[$s+$a]
	a=$[$a+1]
done
echo $s
[root@jiangnan data]# sh until.sh 
5050
[root@jiangnan data]# 

6. Infinite Loop

6.1 Basic syntax

while :
do
	程序
done

or

while true
do
	程序
done

or

for (( ; ; ))
do
	程序
done

6.1 Practical case

[root@jiangnan data]# vi for2.sh 
[root@jiangnan data]# cat for2.sh 
#!/bin/bash
for (( ; ; ))
do 
	echo "I love China"
done
[root@jiangnan data]# sh for2.sh
I love China
I love China
I love China
......

7. Break out of the loop

In the loop process, sometimes it is necessary to force out of the loop when the loop end condition is not reached. Shell uses two commands to achieve this function: break and continue.

7.1 break command

The break command allows to break out of all loops (terminate execution of all subsequent loops).

In the example below, the script goes into an infinite loop until the user enters a number greater than 5. To break out of this loop and return to the shell prompt, use the break command.

#!/bin/bash
while :
do
    echo -n "输入 1 到 5 之间的数字:"
    read aNum
    case $aNum in
        1|2|3|4|5) echo "你输入的数字为 $aNum!"
        ;;
        *) echo "你输入的数字不是 1 到 5 之间的! 游戏结束"
            break
        ;;
    esac
done

Execute the above code, the output result is:

输入 15 之间的数字:3
你输入的数字为 3!
输入 15 之间的数字:7
你输入的数字不是 15 之间的! 游戏结束

7.2 continue command

The continue command is similar to the break command, except that it does not jump out of all loops, only the current loop.

Modify the above example:

#!/bin/bash
while :
do
    echo -n "输入 1 到 5 之间的数字: "
    read aNum
    case $aNum in
        1|2|3|4|5) echo "你输入的数字为 $aNum!"
        ;;
        *) echo "你输入的数字不是 1 到 5 之间的!"
            continue
            echo "游戏结束"
        ;;
    esac
done

Running the code shows that when a number greater than 5 is entered, the loop in this example does not end, and the statement echo "game over" is never executed.

Analogy to break and continue in java

The WeChat public account has been opened first. You can find me by searching for "Jiang Xiaonan and his friends". Friends, you can pay attention. The article will be updated synchronously for easy viewing.

Guess you like

Origin blog.csdn.net/weixin_45842494/article/details/123697154