Learn flow control statements in one minute

What is Linux scripting

1. A Linux script is a sequence of commands that can be used to automate a series of operations. They are often used to automate repetitive tasks and batch processing tasks. In Linux, a script is a plain text file consisting of a series of commands, which contains some specific scripting language formats.

2. Script files usually have an extension of .sh. To enable script files to be executed, execution permissions need to be set for them

Linux Pros and Cons 

advantage:

1. Reduce hands-on ability: A series of operations can be automated, thereby reducing hands-on operations

2. Flexibility: Scripts can be written for different needs, and can be modified or expanded as needed, so they have high flexibility

3. Convenient management: The script is a plain text file, which is easy to perform version control, backup, modification and sharing.

4. Save time: Scripts can perform multiple tasks quickly and accurately, saving time and manpower.

shortcoming:

1. Poor readability: Some complex scripts may be difficult to understand, making maintenance difficult.

2. Security issues: You need to pay attention to the security of the script, because the script can execute any command, so you need to ensure that it will not cause damage to the system.

3. Frequent updates are required: As the system and software versions are updated, scripts also need to be updated continuously to maintain compatibility and usability.

Relational operators:

 

relational operator
-eq
Checks whether two numbers are equal, equal returns true . [ $a -eq $b ] returns false .
- is
Checks whether two numbers are not equal, and returns true if they are not equal . [ $a -ne $b ] returns
true
-gt
Checks if the number on the left is greater than the number on the right, and if so, returns true . [ $a -gt
$b] returns false .
-lt
Checks if the number on the left is less than the number on the right, and if so, returns true . [ $a -lt
$b] returns true .
-ge
Checks whether the number on the left is greater than or equal to the number on the right, and if so, returns true . [ $a
-ge $b ] returns false
- the
Checks whether the number on the left is less than or equal to the number on the right, and if so, returns true . [ $a
-le $b ] returns true
PS: eq: equql equal, equal
gt: greater , greater than
lt: less then less than
Example script:
#!/bin/bash
if [ $1 -eq $2 ]
then
echo "$1 -eq $2 : $1 等于 $2"
else
echo "$1 -eq $2 : $1 不等于 $2"
fi
if [ $1 -ne $2 ]
then
echo "$1 -ne $2 : $1 不等于 $2"
else
echo "$1 -ne $2 : $1 等于 $2"
fi
if [ $1 -gt $2 ]
then
echo "$1 -gt $2 : $1 大于 $2"
else
echo "$1 -gt $2 : $1 不大于 $2"
fi
if [ $1 -lt $2 ]
then
echo "$1 -lt $2 : $1 小于 $2"
else
echo "$1 -lt $2 : $1 不小于 $2"
fi
if [ $1 -ge $2 ]
then
echo "$1 -ge $2 : $1 大于或等于 $2"
else
echo "$1 -ge $2 : $1 小于 $2"
fi
if [ $1 -le $2 ]
then
echo "$1 -le $2 : $1 小于或等于 $2"
else
echo "$1 -le $2 : $1 大于 $2"
fi

shell script

if

ifStatements usually consist of a conditional expression followed by one or more commands. When the conditional expression is true, ifthe command following the statement is executed; otherwise, elsethe command following the keyword, if present, is executed.

Example: script content

#!/bin/bash
echo "请输入一个数字:"
read num

if [[ "$num" =~ ^[0-9]+$ ]]; then
  if [ "$num" -gt 0 ] && [ "$num" -lt 10 ]; then
    echo "输入的数字是一位数。"
  elif [ "$num" -ge 10 ] && [ "$num" -lt 100 ]; then
    echo "输入的数字是两位数。"
  elif [ "$num" -ge 100 ] && [ "$num" -lt 1000 ]; then
    echo "输入的数字是三位数。"
  else
    echo "输入的数字大于等于1000或小于等于0。"
  fi
else
  echo "输入的不是一个有效的数字。"
fi
~         

 2. Execute the script

[root@mysql test]# sh user.sh 
请输入一个数字:
1
输入的数字是一位数。
[root@mysql test]# sh user.sh 
请输入一个数字:
10
输入的数字是两位数。
[root@mysql test]# sh user.sh
请输入一个数字:
100
输入的数字是三位数。
[root@mysql test]# sh user.sh
请输入一个数字:
-10
输入的不是一个有效的数字。
[root@mysql test]# sh user.sh 
请输入一个数字:
1000
输入的数字大于等于1000或小于等于0。
[root@mysql test]# sh user.sh 
请输入一个数字:
0.2
输入的不是一个有效的数字。

if...thenIt is one of the common conditional statements in Bash Shell, which is used to execute different commands according to a certain condition

Note :if...then The command part after the statement can contain multiple commands, just use semicolons to separate multiple commands.

Example: script content

#!/bin/bash
read -p "请输入第一个数字: " num1
read -p "请输入第二个数字: " num2

if [ $num1 -gt $num2 ]
then
    echo "$num1 大于 $num2"
elif [ $num1 -lt $num2 ]
then
    echo "$num1 小于 $num2"
else
    echo "$num1 等于 $num2"
fi

execute script

[root@mysql test]# sh user1.sh 
请输入第一个数字: 10
请输入第二个数字: 20
10 小于 20
[root@mysql test]# sh user1.sh 
请输入第一个数字: 20
请输入第二个数字: 10
20 大于 10
[root@mysql test]# sh user1.sh 
请输入第一个数字: 10
请输入第二个数字: 10
10 等于 10

 if...else

In Linux Shell scripts, if...elsestatements are used to execute different commands based on certain conditions

Note: Aftereach command of theifand, the statement must be terminated, otherwise the code will not be executed correctly.elsefiif...else

example:

Script content 

#!/bin/bash
echo "请输入第一个人的年龄: "
read age1
echo "请输入第二个人的年龄: "
read age2

if [ $age1 -gt $age2 ]
then
  echo "第一个人年龄更大。"
else
  echo "第二个人年龄更大。"
fi
~    

execute script 

[root@mysql test]# sh user2.sh 
请输入第一个人的年龄: 
10
请输入第二个人的年龄: 
15
第二个人年龄更大。
[root@mysql test]# sh user2.sh 
请输入第一个人的年龄: 
18
请输入第二个人的年龄: 
11
第一个人年龄更大。

 if/elif/else judgment structure

 if…elif…elseThe structure is a statement structure used for multiple conditional judgments in Shell scripts

Note: It should be noted that when usingif…elif…elsethe structure, it should be judged in order, that is, first judge whether the first condition is true, if it is true, execute the corresponding command and end the entire structure; if it is not true, continue to judge the next condition , until a certain condition is satisfied

example:

Script content

#!/bin/bash
echo "请输入一个数字: "
read num

if [ $num -gt 0 ]
then
  echo "输入的数字是正数。"
elif [ $num -lt 0 ]
then
  echo "输入的数字是负数。"
else
  echo "输入的数字是零。"
fi
~      

execute script

[root@mysql test]# sh user3.sh 
请输入一个数字: 
0
输入的数字是零。
[root@mysql test]# sh user3.sh 
请输入一个数字: 
10
输入的数字是正数。
[root@mysql test]# sh user3.sh 
请输入一个数字: 
-10
输入的数字是负数。
[root@mysql test]# 

case judgment structure

The case command is a conditional statement in a shell script, which is used to match variable values ​​and perform corresponding operations according to different matching results

example:

Script content:

#!/bin/bash
echo "请输入一个数字"
read num
case $num in
  1)
    echo "你输入了 1"
    ;;
  2|3)
    echo "你输入了 2 或 3"
    ;;
  [4-9])
    echo "你输入了大于等于 4 的数字"
    ;;
  *)
    echo "输入错误"
    ;;
esac

Execute the script: 

[root@mysql test]# sh user4.sh
请输入一个数字
1
你输入了 1
[root@mysql test]# sh user4.sh
请输入一个数字
2
你输入了 2 或 3
[root@mysql test]# sh user4.sh
请输入一个数字
4
你输入了大于等于 4 的数字
[root@mysql test]# sh user4.sh
请输入一个数字
6
你输入了大于等于 4 的数字
[root@mysql test]# sh user4.sh
请输入一个数字
10
输入错误

 Note: In the case command, the branching option can be a single character (such as 1), multiple characters or strings (such as 2|3), or a range (such as [4-9]). And the last * branch is a wildcard, which means to execute when no branch is matched.

function function 

The function can make something similar to a custom execution command in the shell script . The biggest function is that it can simplify a lot of our program codes. If the output is the same, then I can use function to simplify! The syntax of function
Note : Because the shell script is executed from top to bottom, from left to right, so
The function setting in the shell script must be at the front of the program, so that
Enough to find available program segments during execution

Example: script content

#!/bin/bash
read -p "请输入你要查询的装备类别:" INPUT
read -p "请输入装备的分数:" SCORE
function fenshu () {
if [ $SCORE -le 30 ];then
echo "品相极品渣!"
elif [ $SCORE -gt 30 -a $SCORE -le 65 ];then
echo "渣渣!"
elif [ $SCORE -gt 65 -a $SCORE -le 95 ];then
echo "嗯,这才像个样!"
elif [ $SCORE -gt 95 ];then
echo "谁与争锋!"
else
echo "输入有误!"
fi
}


function chongzhu () {
read -p "请问是否选择重铸!Y/N" YN
if [ $YN == Y ];then
read -p "好的,用户选择重铸"
elif [ $YN == N ];then
echo "用户不重铸,886!"
else
echo "输入有误!"
fi
}


case $INPUT in
头盔)
fenshu;chongzhu;;
大剑)
fenshu;chongzhu;;
胸甲)
fenshu;chongzhu;;
*)
echo "没有这个装备";;
esac
~      

 execute script

[root@mysql test]# sh user8.sh 
请输入你要查询的装备类别:头盔
请输入装备的分数:50
渣渣!
请问是否选择重铸!Y/NY
好的,用户选择重铸
[root@mysql test]# sh user8.sh 
请输入你要查询的装备类别:胸甲
请输入装备的分数:100
谁与争锋!
请问是否选择重铸!Y/NN
用户不重铸,886!
[root@mysql test]# 

while loop, until do done (indefinite loop) 

The loops in the shell mainly include for , while , until , and select

while

A while loop in Linux can be used to execute a series of repeated commands until a condition is no longer true.

while [ condition ] // Interpretation of status
do //start of the loop
command
done //end of the loop
The Chinese of while is " when ... " , so this way says " when the condition is established
When, it will loop until the condition of the condition is not true and then stop " means

Example: script content

#!/bin/bash
n=1
while [ $n -le 10 ]
do
    echo "$n"
    n=$((n+1))
done
~     

execute script

[root@mysql test]# sh user5.sh
1
2
3
4
5
6
7
8
9
10

 until loop

The until loop in Linux is similar to the while loop, except that the commands in the loop body are executed when the condition is not true

until [ condition ]
do
command
done
This method is exactly the opposite of while, it says "when the condition condition is true, terminate
cycle, otherwise the program segment of the cycle is continued.
Let's use while to give a small example.

Example: script content

#!/bin/bash
while [ "${yn}" != "yes" ]
do
read -p "请输入'yes'退出: " yn
done
echo "退出成功!."

execute script

[root@mysql test]# sh user6.sh 
请输入'yes'退出: no
请输入'yes'退出: ha
请输入'yes'退出: yes
退出成功!.

 for loop

is the most common loop structure. The for loop is a pre-run test statement, that is, before running any loop body, it is necessary to judge whether the loop condition is true. Only when the condition is true, the loop body will be run, otherwise the loop will exit. After each cycle, the test is repeated before the next cycle. for loop with list

for var in con1 con2 con3
do
command
done
\1. During the first cycle, the content of $var is con1 ;
\2. During the second cycle, the content of $var is con2 ;
\3. In the third cycle, the content of $var is con3

 

Example: script content

#!/bin/bash
for (( n=1; n<=10; n++ ))
do
    echo "$n"
done

execute script

[root@mysql test]# sh user7.sh 
1
2
3
4
5
6
7
8
9
10
[root@mysql test]# 

C - like for loop 

The format is as follows:
for ((expression1; expression2; expression3))
do
command
done
Among them, expression1 is an initialization statement, which is generally used for variable definition and initialization;
expression2 is a judgment expression, which is used to test the return value of the expression and control the loop, return
If the value is true, the loop continues, and if the return value is false, the loop is exited; expression3 is used for variable value modification
Change, thereby affecting the return value of expression2 , and thus affect the loop behavior.

[root@sunday-test shell-script]# cat c_for01.sh
#!/bin/bash
for ((i=1;i<=10;i++))
do
echo -n "$i "
done
[root@localhost ~]# ./t2.sh
12345678910

Summarize:

1. The specialty of the while loop is to execute the daemon process and we hope that the loop will not exit and continue to execute. It is used for loop processing with a frequency of less than one minute. Almost all other while loops can be replaced by for loops.
2. The case statement can replace the if statement. Generally, a small number of fixed rule strings are passed in the system startup script, and the case statement is used . Other common judgments use the if statement.
3. If and for statements are the most commonly used, followed by while ( daemon process ) , case ( service startup script ) and the application scenarios of each statement :
Conditional expressions, simple judgments (whether the file exists, whether the string is empty, etc.).
if value judgment, when the number of different values ​​is small.
For loop normal loop processing, the most commonly used!
while loop daemon, infinite loop ( sleep ).
case service startup script, menu.
The function logic is clear, reducing heavy a

Guess you like

Origin blog.csdn.net/2301_78341899/article/details/131390796