First, Introduction
It is an executable file. Design executable statements in advance to accomplish specific tasks
1.1 Common script types
Bash Shell
Python/perl/ruby
jsp/php/asp/cgi
Javascript
The biggest advantage of 1.2 shell language
Can be fully compatible with Linux systems
1.3 Specification of the composition of shell scripts
#! Environmental Declaration--Declare which interpreter to use/bin/bash
#注释文本 more than 10 lines of script must have comments
Execute code
......
II. Scripting 2.1 Creating a Process 2.1.1 Clear task requirements
-Split small steps in natural language
-Sort them in order.
2.1.2 Writing code files
-How each step is implemented
-Convert to command line save to script file
2.1.3 Testing and perfecting
-Run the script and troubleshoot errors based on the results
-code optimization, user-friendly handling
2.2 Scripting Exercises
-Output Hello World
-Add Yum Source
-Add User test05
-Enter an IP address to determine if it is possible to ping and output the appropriate message
Third, advanced redirection output
1> only receive the correct output from the previous command result
2> only receive error output from previous command results
&> receive the correct output and error output from the previous command result
/dev/null Black hole Equipment
Iv. origin of Variable 4.1
Increased scripting flexibility in order to increase the ability of the script to fit the environment
4.2 Definitions
Store changed values with immutable names
4.3 format
Variable name = changed value
4.4 Precautions
-If the specified variable name exists, it is equivalent to re-assigning a value to the variable
-cannot have spaces on either side of the equals sign
-Variable names can only be made up of numbers/letters/underscores
-variable names cannot start with a number, do not use keywords and special characters
4.5 Viewing methods
Echo Variable Name
echo {variable name}: Enclose the variable with {} when the variable and constant are output together
4.6 Citation method
$ variable Name
${variable Name}
4.7 Variable classification 4.7.1 environment variables
Variable names are generally capitalized to set the user/system environment
$USER: currently logged in user
$PWD: Current Working directory
$PATH: Provides a search path for the command program
4.7.2 position Variable
Bash built-in to store command-line arguments that are provided when the script executes
With $n, n is the ordinal number, such as 1,2,3,4, ...
-Positional variables must be referenced in the form of ${12} when in even-numbered
4.7.3 pre-defined variables
Bash built-in, special values that can be called directly, not directly assigned to these variables
$#: The number of position variables that have been loaded
$*: Values for all positional variables
$?: Status value when program exits, 0 indicates normal, other value is exception
$: Current process or pin name
$$: PID number of the current running process
4.7.4 Custom Variables
User-defined variables
Five, Shell Common Command 5.1 read-p ' Input username: ' User
Read the input of the keyboard by line, assign the input to User,-p is the printing prompt information
5.2 Expr 2 * 3
Evaluates the value of the expression as 2*3, with a value of 6
5.3 $ (command)
In a sequence of instructions, the instruction in the $ (command) is executed first, the result of execution is returned to the external instruction to handle
$ (Date +%f): current date
5.4
command
Same effect as $ (command)
5.5 Shift [...]
The shift command is used to move a parameter (left) and is typically used to traverse each parameter in turn without knowing the number of incoming arguments and then handle it accordingly (common to startup scripts for various programs in Linux). Shift with no parameters equals shift 1
DAY11 Shell Script Basics (enginner05-1)