The Bash shell script programming knowledge points are as follows (beginner, incomplete, welcome to the discussion supplement):
Shell Introduction
A simple introduction to scripts
Variables and references
Arithmetic operations
Interactive programming
Select Judgment
Condition test
Cycle
Function
Shell Introduction
Shell is a special function of the program, provides a user to interact with the kernel interface, it receives the user input commands, and put it into the kernel to execute, the shell is divided into graphical interface and command interface (the window system we operate is a graphical shell, We want to learn bash is also she a command interface shell).
The shell's diagram with the kernel and the user is as follows:
Bash Shell features:
Provides an interactive interface:
Command line expansion: ~,{}
Command alias: Slias,unalias
Command: History
globbing:*,?,[],[^]
Shortcut keys
Command completion: $PATH
Command Hash
Path completion
Provides programming environment: process-oriented, scripted programming
Programs: Statements and expressions
Process:
Sequential execution
Select Execute
Loop execution
Programming Model: Process oriented, object oriented
program = instruction + data
Process oriented: organization of data as an instruction center. Bash Shell is a process-oriented programming
Object oriented: Data-centric organization of directives.
Program Run Model:
Compile run:
Source--preprocessing----compile----Link
Explanation Run: Bash is the explanation of the run
Interpreter/bin/bash
SOURCE program
First line of the script: Shebang
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
A simple introduction to scripts
The advantage of scripting language is simple, easy to learn, easy to use, suitable for dealing with objects such as files and directories, in a simple way to quickly complete some complex things. It is suitable for the system management, text processing and other aspects of the completion of specific functions of small programs.
- writing format; Shell script format:
#!/bin/bash must be written shelf to indicate the type of interpreter. In addition to the first line of #! indicating the interpreter type, all the # lead shells are considered comments and will not be executed.
- Execution and detection
-N grammar test, test whether there is a syntax error, just learn shell recommendations before executing using the-n test, to ensure that the syntax is wrong.
The-X simulation executes to show each step in the script execution. Easy to find errors.
With the plus sign is the step to perform, without-X does not display a line with a plus sign.
Variables and references:
A variable is a named address space, and memory is an addressable storage unit:
Variables are used to save useful information, such as path names, filenames, numbers, etc., and Linux users use variables to customize their work environment so that the system learns about user-related configurations. A variable is essentially a stored memory address of one or more computers.
Variable type:
local variable : Valid only for the current shell process, not valid for other shells other than the current shell (including its parent, child, and so on). And it is invalid as the shell process dies.
Local Variables : Valid only within a piece of code space in the current shell process, usually for function local programs.
Environment Variables : Valid for the current shell process and its child shell processes;
positional parameter variable : $1,$2 .....
Special variable : $?,$0,$#,$$,$*,[email protected]
Variable assignment and reference:
Local Variables: You do not need to declare directly to use, for example, your home's things directly to use, and other people's things you have to say to use the same.
Variable assignment: [set] Var_name=value set can be omitted, direct variable name = variable value assignment complete.
Variable reference: ${var_name} or $var_name {} curly braces can be omitted, which is used when the variable is not followed by the variable name
Direct assignment is directly used. Curly braces can be omitted at this time.
"": A weak reference in which the variable is replaced and most of the characters are considered ordinary characters. Exceptions also have \ $ ' Three special characters that can be used without escaping.
': A strong reference , any character is treated as a normal character, except the single quotation mark itself. So it is not possible to include single quotes between two single quotes, or escape with \.
· · : Command Reference : the Keyboard tab key above the character, referring to the command.
A strong reference is not the same as the result of a weak reference
Set: Displays all local variables.
Unset var_name: Destroys a variable.
Environment Variables :
Variable assignment:
Export Var_name=value
Declare–x Var_name=value
Export Var_name: You can assign a local variable to an environment variable, provided that the local variable must exist. Also cannot add $ sign
Declare–x Var_name: Ibid.
Local variable to environment variable conversion.
Variable reference: ${var_name} or $varname with local variable reference. Curly braces can also be omitted.
Note: There are many environment variables built in bash
Histsize,histfile,histcontrol,path,home,shell
Export: Show all environment variables; printenv,env can also display
unset var_name: Destroying environment variables
Read-only variables:
ReadOnly var_name changes the environment variable to a read-only variable
Declare–r Var_name Ibid.
Positional parameter variables: Often used in interactive scripting. The first digit of the input is $ $,
Variable command rule:
- Cannot bring the keyword in the program: If,elif,fil,then,for, ...
- Numbers, letters, or underscores cannot be used, and cannot begin with a number;
- See the meaning of the variable when you see it.
Syntax knowledge points for Bash Shell programming (1)