Detailed explanation and practice of Linux shell script -- a branch of shell script

Keywords: Linux shell bash script

Today, I will continue to introduce you to the basic knowledge of Linux. The main content of this article is the branch of Linux shell script.

1, shell branch -- if statement form

In shell script, we usually use if statement to make the script execute different commands according to some conditions, which can greatly make the function of the script more powerful and flexible.
if statement is the command to implement branch in shell script. if script has three forms: single branch, double branch and multi branch. The formats of these three forms are as follows:
if single branch:

if expression; then
     Execute statement
fi

if double branch:

if expression; then
     Execute statement
else 
     Execute statement
fi

if multiple branches:

if expression; then
     Execute statement
elif expression;then 
     Execute statement
elif expression;then 
     Execute statement
............
else 
     Execute statement
fi

2, shell branch - if expression

In an if script, we need to write an if expression to indicate the conditions of the if script. If expressions often use square brackets. The contents of the expression can include logical expressions, special operation comparators (not +, -, *...), i.e. special judgment parameters, etc.
Common judgment operators in if expressions are as follows:

-f    Determine whether the file exists
-d    Determine whether the directory exists
-z    Determine whether it is an empty string

Common logical operators in if expressions are as follows:

-eq   Compare whether the two sides are equal
-ne   Compare whether the two sides are not equal
-lt   Compare whether the left is smaller than the right
-gt   Compare whether the left is larger than the right
-le   Compare whether the left is less than or equal to the right
-ge   Compare whether the left is greater than or equal to the right
-a or &&  Two logical expressions are connected on the left and right sides. This symbol represents logical and
-o or ||  Two logical expressions are connected on the left and right sides, and the symbol represents logical or

It should be noted that the above symbols apply to the case where the if expression is a bracket "[]". If the if expression uses parentheses, arithmetic operators such as +, -, * and so on are used in the brackets. The parentheses in the shell script are explained in detail below.

3, shell branch - if bracket

In shell scripts, understanding the parentheses can help us understand the scripts written by others. When we write our own scripts, we can choose the format we like. There are five types of parentheses in the shell. They are braces, double brackets, square brackets, double parentheses, and small brackets
1. Braces
Braces are generally used to expand commands. For example, the touch command can create multiple files at one time with the help of braces. The commands are:

touch file{1..10..2}

In a shell script, if a function is defined, the execution statement of the function also needs to be enclosed in braces.
2. Bracket
The use of brackets is just like the usage introduced in Chapter 2 of this article, which is used for conditional expressions of if statements
3. Double bracket
Double square brackets support the usage of square brackets in Section 2 of this article, and also support mathematical logic operators such as = =, >, <, that is, double square brackets have higher fau lt tolerance and more flexible format.
4. Double parentheses
Double parentheses can also be used for if expressions, and support symbols such as = =, >, > =, & &, which is more in line with C language programming habits.
5. Parentheses
Parentheses are often used in for loops to specify loop conditions and arrays. In addition, commands can be placed within parentheses, which need to be separated by semicolons.

4, shell branch - script example

Finally, write several simple shell script examples for beginners to learn.

(1) Determine whether the specified directory exists

#!/bin/bash
# 2021-10-13
# Author:Pzz
# Used to practice shell script
# This script is used to find whether the dir is existed
if [ -d /var/www/html ];then
        echo "The web dir is existed"
fi
if [ ! -d /var/www/hdjksjk ];then
        echo "The other dir is not existed"
fi

(2) Compare the size of two numbers

#!/bin/bash
# 2021-10-13
# Authored bu Pzz
# This script is uesed to find the bigger numa
read -p "Please input the first number:" NUM1
read -p "Please input the second number:" NUM2
if [ $NUM1 -gt $NUM2 ];then
    echo "The $NUM1 is larger"
else
    echo "The $NUM2 is larger"
fi
~      

(3) Grading of test results

#/bin/bash
# 2021-10-13
# Authored by Pzz
# This script is used to get the grade
# This script din't test the user's input
SCORE=$1
if [ $SCORE -gt 90 ];then
  echo "A"
elif [ $SCORE -gt 75 ];then
  echo "B"
elif [ $SCORE -gt 60 ];then
  echo "C"
else
  echo "D"
fi

Welcome to continue to follow my blog and will continue to update more complex and useful shell scripts!!
It's not easy to be original. Please explain the source: https://blog.csdn.net/weixin_ forty million two hundred and twenty-eight thousand and two hundred

Posted by warik on Wed, 13 Oct 2021 07:11:18 -0700