Judgment statement of shell script programming

Time:2021-10-10

1、 Single branch if statement

Format: if judgment condition; then


statement1
statement2
...
fi

2、 Double branch if statement

Format: if judgment condition; then


statement1
statement2
...
else
statementN
...
fi

Use a previously used script to illustrate this structure.

3、 Multi branch if statement

Format: if judgment condition 1; then

statement1
...
Elif judgment condition 2; then
statement2
...
Elif judgment condition 3; then
statement3
...
else
statement4
...
fi

4、 Case statement

Format: case variable in


PATTERN1)
statement
...
;;
PATTERN2)
statement
... 
;;
*)
statement
...
;;
esac

Because the judgment structure statement is relatively simple and does not make too much explanation, just remember the structure and usage of each statement.

Supplementary shell basic syntax

1.1. Beginning of shell file

The shell file must start with the following line (must be on the first line of the file):
#!/bin/sh

Symbol #! Used to tell the system that the following parameters are the program used to execute the file. In this example, we use / bin / sh to execute the program.

When you edit a script, you must also make it executable if you want to execute it.

To make a script executable:

Run Chmod + X filename to run with. / filename

1.2 notes

In shell programming, a sentence beginning with # indicates a comment until the end of the line. We sincerely recommend that you use comments in your program.

If you use comments, even if you haven’t used the script for a long time, you can understand the role and working principle of the script in a short time.

1.3 variables

In shell programming, all variables are composed of strings, and you don’t need to declare variables. You can assign values directly. If you apply variables, use the form of $+ variable name.

To assign a value to a variable, you can write:


a="hello world" 

Now print the contents of variable a:


echo "A is:" 
echo $a

Sometimes variable names are easily confused with other words, such as:


num=2 
echo "this is the $numnd" 

This does not print “this is the 2nd”, but only “this is the”, because the shell will search for the value of the variable numnd, but this variable has no value. You can use curly braces to tell the shell that we want to print the num variable:


num=2 
echo "this is the ${num}nd" 

This will print: This is the 2nd

1.4 environmental variables

Variables processed by the export keyword are called environment variables. We won’t discuss environment variables, because usually we only log in
Use environment variables in scripts.

Recommended Today

Centos8.4 installing confluence7.13 md

Reference 1:https://blog.whsir.com/post-5854.htmlReference 2:https://confluence.atlassian.com/confkb/confluence-basic-nginx-configurations-for-proxy-load-balancing-logging-1019381716.htmlReference 3:https://confluence.atlassian.com/conf73/running-confluence-behind-nginx-with-ssl-991928804.html System environment and installation preparation System version database Application software CPU Memory CentOS 8.4 MySQL 8.0.21 Confluence 7.13.0 16 cores 16G Confluence 7.13 download from the official website MySQL drive connection Cracking tool download jdk-8u301-windows-x64.exeThis is to run the cracking tool #Kernel parameter optimization VIM /etc/sysctl conf net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter […]