First, Introduction
Shell two command execution mode:
1. Interactive: single Command execution
2. Batch processing: script
Shell scripts are interpreted and do not need to be compiled
tips: try to use a regular user account
Second, the Interpreter
Role: Used to execute scripts
1. Bash
Bash is the Linux standard default Shell, completed by Brian Fox and Chet Ramey, and is the abbreviation for Bourneagain Shell, which has a total of 40 internal commands.
2. Sh
SH was developed by Steve Bourne and is the abbreviation for the Bourne Shell, which is the default shell of the UNIX standard.
3. Ash
Use less resources, including 24 commands, inconvenience
4. csh
CSH is a Linux-based kernel that consists of 47 authors, represented by William Joy, and has 52 internal commands. The shell is actually a shell that points to/bin/tcsh, which means that csh is actually tcsh.
5. Ksh
Ksh is an abbreviation for the Korn shell, written by Eric Gisin, with a total of 42 internal commands. The biggest advantage of the shell is that it is almost completely compatible with the ksh of the commercial distribution, so you can try out the commercial version of the performance without having to pay for the commercial version.
tips:BASH is the acronym for the Bourne Again shell, the default shell for the Linux standard, which is based on the Bourne shell and absorbs some of the features of the C shell and Korn shell. Bash is fully compatible with SH, meaning that scripts written in SH can be executed without modification in bash.
Third, the characteristics
The advantage of using scripting languages is that they are mostly run at a higher level than the compiled language and can easily handle objects such as files and directories, and the disadvantage is that they are often less efficient than compiled languages. However, the tradeoff is that scripting is usually worthwhile: a simple script that takes one hours to write, the same functionality written in C or C + +, can take up to two days, and, in general, the speed of the script execution is fast enough to let people ignore its performance problems. Examples of scripting languages are awk, Perl, Python, Ruby, and Shell.
Given the command restrictions and efficiency of shell scripts, the following situations do not typically use the shell:
- resource-intensive tasks, especially when you need to consider efficiency (e.g., sorting, hashing, etc.).
- Mathematical operations that need to handle large tasks, especially floating-point operations, precision operations, or complex arithmetic operations (which are typically handled using C + + or FORTRAN).
- There are cross-platform (operating system) porting requirements (typically using C or Java).
- Complex applications where structured programming must be used (requires variable type checking, function prototypes, etc.).
- For mission-critical applications that affect the overall system.
- Tasks that require a high level of security, such as requiring a robust system to prevent intrusion, cracking, malicious destruction, and so on.
- A project consists of various parts of a chain of dependencies.
- Large-scale file operations are required.
- Support for multidimensional arrays is required.
- Support for data structures, such as linked lists or numbers, is required.
- Graphical interface GUI needs to be generated or manipulated.
- Requires direct operating system hardware.
- I/O or socket interface required.
- Interfaces that require the use of libraries or legacy old code.
- Private, closed-source applications (shell scripts put the code in a text file, as the world can see it).
Iv. scripting
1. As an executable program
#!/bin/bash
#! Convention Interpreter
The extension is. sh
Execute with./or source
2. As an interpreter parameter
This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:
$/bin/sh test.sh
$/bin/php test.php
Five, Shell variables
1. Define variables:
When defining a variable, the variable name does not have a dollar sign ($), such as:
Variablename= "Value"
Note that there can be no spaces between the variable name and the equals sign
- The first character must be a letter (a-z,a-z).
- You can use an underscore (_) without spaces in the middle.
- Punctuation cannot be used.
- You can't use the keywords in bash (you can see the reserved keywords using the help command).
2. Using variables
With a defined variable, just precede the variable name with a dollar sign ($), such as:
Your_name= "Mozhiyan"
Echo $your _name
Echo ${your_name}
The curly braces outside the variable name are optional and add no lines, and curly braces are used to help the interpreter identify the bounds of the variable.
3. Redefining variables
A defined variable can be redefined, such as:
Myurl= "http://see.xidian.edu.cn/cpp/linux/"
Echo ${myurl}
Myurl= "http://see.xidian.edu.cn/cpp/shell/"
Echo ${myurl}
4. read-only variables
Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.
5. Deleting variables
Use the unset command to delete a variable.
- Unset variable_name
The variable cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.
6. Variable type
When you run the shell, there are three different variables:
1) Local Variables
Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
2) Environment variables
All programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
3) Shell variables
Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.
7. Special variables
Shell $, $#, $*, [email protected], $?, $$ and command-line arguments
List of special variables
variables |
meaning |
$ |
File name of the current script |
$n |
Arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A. |
$# |
The number of arguments passed to the script or function. |
$* |
All parameters passed to the script or function. |
[Email protected] |
All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below. |
$? |
The exit state of the last command, or the return value of the function. |
$$ |
The current shell process ID. For Shell scripts, this is the process ID where the scripts are located. |
The difference between $* and [email protected]
$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n".
But when they are enclosed in double quotation marks (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".
Six, Shell variable substitution
Shell variable substitution, command substitution, escape character
The following escape characters can be used in echo:
Escape Character |
meaning |
\\ |
Back slash |
\a |
Alarm, Bell |
\b |
BACKSPACE (delete key) |
\f |
Page Break (FF), moving the current position to the beginning of the next page |
\ n |
Line break |
\ r |
Enter |
\ t |
Horizontal tab (Tab key) |
\v |
Vertical tab |
You can suppress escaping by using the-e option of the Echo command, which is not escaped by default, or by using the-N option to prevent the insertion of line breaks.
Shell Script 1.0