Programming Fundamentals and Scripting Specifications

1. Programming basics

1.1 Program Composition

Program: Algorithm + Data Structure

  • Data: is the core of the program
  • Algorithms: Ways to Process Data
  • Data structures: the types and organization of data in a computer

1.2. Programming style

1. Procedural style: Code is organized around instructions, and data serves instructions. Representative: c language, bash.
There are three types of execution logic in procedural programming languages:

  • Sequential execution: programs are executed sequentially from top to bottom
  • Select execution: During program execution, according to different conditions, select different branches to continue execution
  • Loop execution: A certain statement needs to be executed repeatedly during program execution
    2. Object-based: Data-centered to organize code, instructions serve data, representative: java, c++, Python
    • class: instantiated object, method

1.3 Classification of programming languages

According to the mode of operation

  • Compile and run: source code-compiler----program file
  • Interpretation and operation: source code----the interpreter is started at runtime, and the interpreter runs while interpreting

According to the implementation of the function in the startup compilation process, whether to call the library or call the external program file

  • Shell script programming: programming using commands and programming components on the system
  • Complete programming: Compile with library or compiler components

2. Shell script basics

2.1 Purpose and application scenarios of shell script

use:

  • Automate Common Commands
  • Perform system administration and troubleshooting
  • Create simple application
  • Processing text or files
    2.2 application scenarios:
  • repetitive operation
  • interactive task
  • batch transaction processing
  • Service running status monitoring
  • Timing task execution

2.2 Common shell interpreters

  • The shell in the Linux system is a special application program, which is between the operating system kernel and the user, and acts as a "command interpreter", responsible for receiving and interpreting the operation instructions entered by the user, and passing the operations that need to be performed Execute the kernel and output the result. It is a bridge for users to use linux
  • The shell is not only a command language, but also a programming language
  • There are many kinds of common shell interpreter programs. When using different shells, there will be some differences in their internal instructions and command line prompts. You can know the types of shell scripts supported by the current system through the /etc/shell file

View native shell information:
insert image description here
Common shells in linux

  • bash: a shell developed under the framework of GUN
  • csh: a shell similar to c language
  • tcsh: integrates csh to provide more functions
  • sh: has been replaced by bash
  • nologin: prevent users from logging in to
    bash (bin/bash) is the default shell used by most linux versions

Bash features
1. Support shortcut keys: such as ctrl+c to force the termination of the process, ctrl+l to clear the screen, tab completion, and so on.
2. Support production viewing history commands
3. Support aliases
4. Redirection of standard input and standard output
5. Pipe character
6. File name wild card mechanism
7. Support command hash
8. Support variables

These functions are unique to bash, and other shell environments do not have these functions, or they are not so complete, so bash replaces sh as the default shell of linux

2.3 Basic structure of shell script

A shell script is a program file that pre-puts various commands into a file for one-time execution. It is mainly convenient for administrators to set or manage

What is a shell script :

  • List the commands to be executed one by one in order, save them to a text file, and finally execute them automatically
  • X permissions are required to execute scripts, and absolute paths can also be used to execute files
  • Can be combined with various shell control statements to complete more complex operations

Script composition:
1. Step statement (declare interpreter): the first line begins with "#!/bin/bah", indicating that the code statement below this line is executed through the /bing/bash program. There are other types of interpreters, such as #/usr/bin/python, #! /usr/bin/expect
2. Comment information: Statements beginning with # are expressed as comment information, and the commented statements will not be executed when the script is running
3. Executable statements: such as the echo command, used to output between "" String

2.4 shell specification script

Script name specification: the file name ends with .sh, which is easy to identify.
The script code begins with conventions:
1. The first line is generally the language used for calling.
2. The program name, so as not to find the correct file after changing the file name
. 3. Version number
. The time after the change
5. Relevant information of the author
6. The function of the program and precautions
7. Finally, the update time of each version

2.5 Execution method of the script

1. Specify the path to find the executable file, the file needs to have execution permission

  • absolute path. Such as: /root/hello.sh
  • relative path. Such as: ./hello.sh
    insert image description here
    2. Specify the interpreter to execute (bash script name), no execution permission is required
  • bash script name. Such as: bash hello.sh
  • "source script name" h or '".script name". // will not start the subshell environment

insert image description here
Note;
1. The shell environment when executing the script:

  • When source and . execute the script, the script will be executed in the current shell environment
  • bash, absolute path, relative path, when executing a script, a subshell environment will be created, and the script will be executed in this subshell environment 2. It is not recommended to
    use source to execute the script, which may affect some resource configurations
    3. Blanks in the script The line will be ignored by the interpreter
    4. In the script, except for shebang, all remaining lines starting with # will be regarded as comment lines and ignored, that is, comment lines

2.6 Script Error Debugging

Types of script errors:
1. Command errors: command errors will not affect the next command, and subsequent commands will continue to execute
2. Syntax errors: will cause subsequent commands not to be executed. Some commands in the script have been executed, and some have not been executed
. 3. Logic error: the effect after execution is not what you want. need to check by yourself

bash -n 脚本名称     //只检查语法错误,不真正执行脚本,定位的错误行可能不准确
bash -x 脚本名称     //显示每个命令的执行过程,方便发现逻辑板错误

3. Redirection and pipe characters

Due to the particularity of "batch processing" of shell scripts, most of its operations are in the background and do not require user intervention. Therefore, it is very important to learn to extract and filter execution information, so we need redirection and pipelines

3.1 Standard input to output

When the shell command is executed, 3 folders will be opened by default, and each folder has a corresponding file descriptor for our convenience

type device file File Description default Corresponding file location
standard input /dev/stdin 0 get input from keyboard proc/self/fd/0
standard output /dev/stdout 1 output to screen proc/self/fd/1
error output /dev/stderr 2 output to screen /proc/self/fd/2

Interactive hardware device:

  • standard input: receive user input data from this device
  • Standard output: the data output to the user through the device
  • Standard error: report execution error information through this device

3.2 Redirection

1. Redirection means that instead of going through the standard output to the default screen, it outputs to the location you specify
Output redirection

symbol effect
command > file Save the standard output result to the specified file
command>>file Append the standard output result to the end of the specified file
command 2 > file Save the error output information to the specified file
command 2>>file Append error output information to the end of the specified file
command>file2>&1 Mixed output, saving standard output and error output to the same file
Note:
  • & means mixed, &> and >& both mean to redirect standard output and standard error output to the same file
  • Command>file 2>&1 means to redirect the error output 2 to the previous standard output 1, that is, to save the error output and standard output to the same file
    insert image description here
  1. Redirect 'aaa' to the fa.txt file, then output 'bbb' to the fa.txt file, overwrite the original 'aaa', and finally append 'ccc' to the fa.txt file 3.
    insert image description here
    Use Delimiter for input redirection
    insert image description here
    4. Mixed output, redirect standard output and error output to the file at the same time
    insert image description here

3.3 Pipe character

The function of the pipe character is to connect two commands, and the standard output of the first command is used as the standard input of the second command. Multiple pipe characters can be used in the same command line

The pipe character under Linux is represented by |

insert image description here

Guess you like

Origin blog.csdn.net/fyb012811/article/details/132192307