Chapter 6 Introduction to Shell script programming

Keywords: Linux Operation & Maintenance architecture DevOps Cloud Native

1. Fundamentals of programming

Linus: Talk is cheap, show me the code

1.1 procedure composition

  • Program: algorithm + data structure
  • Data: is the core of the program
  • Data structure: the type and organization of data in a computer
  • Algorithm: how to process data

1.2 programming style

  • Process oriented language
    • To do one thing, arrange two steps: what to do in the first step, what to do in the second step, what to do if situation A occurs, and what to do if situation B occurs
    • The problem is small in scale and can be handled step by step
    • Instruction centered, data serving instruction
    • C,shell
  • Object oriented language
    • Regard programming as a thing. For the outside world, things are used directly and don't care about the internal situation of things. Programming is to set things to complete functions.
    • A methodology for understanding and analyzing the world. Abstract everything into various objects
    • Class is an abstract concept, an abstraction of everything, and a collection of common characteristics of a class of things
    • An object is a concrete representation of a class and an entity
    • Large scale and complex system
    • Data centric, instructions serve data
    • java, C#, python, golang, etc

1.3 programming language

Computers: running binary instructions

Programming language: the language of interaction between human and computer. There are two kinds: low-level language and high-level language

  • Low level programming languages:
    • Machine: a binary sequence of 0 and 1, called machine instructions. It is too different from natural language to understand and write
    • Assembly: using some mnemonic symbols instead of machine instructions is called assembly language
      • For example, add A and B add the number of register A and the number of register B to register A
      • The program written in assembly language needs to be converted into machine instructions
      • Assembly language is a little easier to understand, that is, the mnemonic corresponding to machine instructions is closer to natural language
  • Advanced programming language:
    • Compilation: high level language – > compiler – > machine code file – > execution, such as: C, C++
    • Explanation: high level language – > execution – > interpreter – > machine code, such as shell, python, php, JavaScript, perl

Compiled and interpreted language


1.4 programming logic processing mode


Three kinds of processing logic

  • Sequential execution: the program is executed from top to bottom
  • Select execution: during program execution, select different branches to continue execution according to different conditions
  • Circular execution: a certain statement needs to be executed repeatedly during program execution

2. Basic usage of Shell Scripting Language

2.1 purpose of shell script

  • Combine simple commands to complete complex work, automatically execute commands and improve work efficiency
  • Reduce the repeated input of manual commands and avoid human errors to a certain extent
  • Standardize the installation and configuration of software or applications
  • It is used to realize daily and repetitive operation and maintenance work, such as file packaging, compressed backup, monitoring system operation status and realizing alarm, etc

2.2 basic structure of shell script

shell script programming: it is a language based on procedural interpretation and execution

Basic structure of programming language:

  • Combination of various system commands
  • Data storage: variables, arrays
  • Expression: a + b
  • Control statement: if

shell script: a text file that contains some commands or declarations and conforms to a certain format

Format requirements: shebang mechanism on the first line

#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl

2.3 shell script creation process

Step 1: use a text editor to create a text file

The first line must include the shell declaration sequence: #!

Example:

#!/bin/bash

Add a comment that begins with #

Step 2: add execution permission

Give execution permission and specify the absolute or relative path of the script on the command line

Step 3: run the script

Run the interpreter directly and run the script as a parameter of the interpreter program

2.4 shell script annotation specification

1. The first line is generally the language used for the call

2. Program name to avoid changing the file name. The correct file cannot be found

3. Version number

4. Time after change

5. Author related information

6. Function and precautions of the procedure

7. Finally, a brief description of the update of each version is given

2.5 first script

#!SHEBANG
CONFIGURATION_VARIABLES
FUNCTION_DEFINITIONS
MAIN_CODE

Example: the first Shell script hello world

Reference documents:

https://zh.wikipedia.org/wiki/Hello_World

https://zh.wikipedia.org/wiki/Hello_World%E7%A8%8B%E5%BA%8F%E6%A0%B7%E4%BE%8B

[root@rocky8 ~]# echo $SHELL
/bin/bash

[root@rocky8 data]# vim hello.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-08
#FileName:      hello.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
#Classic writing
echo "hello, world"
#Popular writing
echo 'Hello, world!'

#Execution method 1
[root@rocky8 data]# bash hello.sh 
hello, world
Hello, world!

#Execution method 2
[root@rocky8 data]# cat hello.sh | bash
hello, world
Hello, world!

#Execution method 3
[root@rocky8 data]# chmod +x hello.sh 
#Absolute path
[root@rocky8 data]# /data/hello.sh 
hello, world
Hello, world!
#Phase normal path
[root@rocky8 data]# ./hello.sh 
hello, world
Hello, world!

#Execute method 4, which can execute the shell script of the remote host
[root@rocky8 data]# yum -y install httpd
[root@rocky8 data]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@rocky8 data]# mv hello.sh /var/www/html/
[root@rocky8 data]# curl -s http://172.31.1.8/hello.sh | bash
hello, world
Hello, world!

Example: backup script

[root@rocky8 data]# vim backup.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      backup.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
echo -e "\033[1;32mStarting backup...\033[0m"
sleep 2
cp -av /etc/ /data/etc`date +%F_%H-%M-%S`/
echo -e "\033[1;32mBackup is finished\033[0m"

[root@rocky8 data]# bash backup.sh 
Starting backup...
'/etc/' -> '/data/etc2021-10-08_18-39-03/'
'/etc/dnf' -> '/data/etc2021-10-08_18-39-03/dnf'
...
'/etc/nftables/nat.nft' -> '/data/etc2021-10-08_18-39-03/nftables/nat.nft'
'/etc/nftables/router.nft' -> '/data/etc2021-10-08_18-39-03/nftables/router.nft'
'/etc/rsyslog.d' -> '/data/etc2021-10-08_18-39-03/rsyslog.d'
Backup is finished
[root@rocky8 data]# ll
total 16
-rw-r--r--   1 root root  561 Oct  8 18:38 backup.sh
drwxr-xr-x. 84 root root 8192 Oct  8 18:35 etc2021-10-08_18-39-03

2.6 shell script debugging

Only syntax errors in the script are detected, but command errors cannot be detected, but the script is not really executed

bash -n /path/to/some_script

Commissioning and execution

bash -x /path/to/some_script

example:

[root@rocky8 data]# vim test.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      test.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
hostname
cat >app.conf <<EOF
line1
line2
line3
EOF
whoami
[root@rocky8 data]# bash test.sh 
rocky8
root
[root@rocky8 data]# ll app.conf 
-rw-r--r-- 1 root root 18 Oct  9 13:49 app.conf
[root@rocky8 data]# cat app.conf 
line1
line2
line3

[root@rocky8 data]# vim test.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-09
#FileName:      test.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
hostname
cat >app.conf <<EOF
line1
line2
line3
line4
EOF  #If you enter a space after EOF
whoami
[root@rocky8 data]# bash test.sh 
rocky8
test.sh: line 18: warning: here-document at line 13 delimited by end-of-file (wanted `EOF')
[root@rocky8 data]# cat -A test.sh
#!/bin/bash$
#$
#**********************************************************************************************$
#Author:        Raymond$
#QQ:            88563128$
#Date:          2021-10-09$
#FileName:      test.sh$
#URL:           raymond.blog.csdn.net$
#Description:   The test script$
#Copyright (C): 2021 All rights reserved$
#*********************************************************************************************$
hostname$
cat >app.conf <<EOF$
line1$
line2$
line3$
line4$
EOF $
whoami$
#Use cat -A to view invisible characters, or enter vim and enter: set list

[root@rocky8 data]# bash -n test.sh 
test.sh: line 18: warning: here-document at line 13 delimited by end-of-file (wanted `EOF')
#Check the syntax first with bash -n

[root@rocky8 data]# vim test.sh
#!/bin/bash$
#$
#**********************************************************************************************$
#Author:        Raymond$
#QQ:            88563128$
#Date:          2021-10-09$
#FileName:      test.sh$
#URL:           raymond.blog.csdn.net$
#Description:   The test script$
#Copyright (C): 2021 All rights reserved$
#*********************************************************************************************$
hostname$
cat >app.conf <<EOF$
line1$
line2$
line3$
line4$
EOF$                             
whoami$
[root@rocky8 data]# bash -n test.sh 
[root@rocky8 data]# bash test.sh 
rocky8
root

[root@rocky8 data]# bash -x test.sh 
+ hostname
rocky8
+ cat
+ whoami
root
#bash -x can trace the execution of commands

Summary: there are three common script errors

  • Syntax error will cause subsequent commands not to continue to execute. bash -n can be used to check the error. The number of error lines prompted may not be accurate
  • If the command is wrong, the subsequent commands will continue to be executed by default. bash -n cannot be used to check it. bash -x can be used to observe it
  • Logical error: observe with bash -x

Posted by mitsubishi2002 on Thu, 21 Oct 2021 17:09:36 -0700