First, Shell introduction
------------
User
|
Terminal (equivalent to Bash's interface)
|
Shell (shell) bash most Linux systems default shell environment
|
Kernel
The user enters the command through the terminal, submits it to the shell, and then invokes the system function in the kernel to execute the command. The shell environment is a bridge for users to use Linux, which enables them to interact with the core functions of the operating system.
Two ways to execute commands in the shell:
Interactive: Explains the command that executes the user, the user enters a command, and the shell executes a single line.
Batch processing: The user writes a shell script (scripts) in advance, which has many commands, allowing the shell to execute the commands at once without having to strike a single command.
Writing scripts
Open a text editor and create a new file test.sh
#! /bin/bash--#! is a contract tag that tells the system what interpreter this script needs to execute, and/bin/bash is a Bash interpreter command path
echo "What's your name?"
echo "My name is Lily."
echo "Hi lily!"
Execute script
chmod +x./test.sh--script in the current directory, you can also use absolute path
./test.sh
You can also use SH./test.sh directly
Second, shell variables
------------
1. Defining variables
Variable name = "Variable Contents"
such as: Myname= "Lily"
Precautions:
1) There can be no spaces between the variable name and the equals sign and the equals sign and the variable contents
2) variable names can only contain numbers, letters, and underscores, and must start with a letter
3) variable names cannot use the keywords in bash
2. Using variables
$ variable name--Get variable contents
such as: Echo $myname
or Echo ${myname}
echo "MyName is $myname"
3. Variable type
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.
Local variables: Local variables are defined in a script or command and are valid only in the current Shel instance, and other shell-initiated programs cannot access local variables.
Environment variables: All programs, including shell-initiated programs, have access to environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.
Special variables: Except for numbers, letters, and underscores, variables that contain other characters become special variables, as follows:
$ $ current Script
$n arguments passed to a script or function, n is a number that represents the first parameter, for example, that is $ $, and the second argument is the $
$# the number of arguments passed to a script or function
$* all parameters passed to a script or function
[email protected] All parameters passed to the script or function
$? The exit state of the last command, the last command succeeds, returns 0, and the value of 0 is not returned.
$$ the current shell process ID. For shell scripts, this is the process ID where the script resides.
--Examples:
Vim var.sh--Create a new shell script
#! /bin/bash
echo "File name: $"
echo "Parameter 1:$1"
echo "Parameter 2:$2"
echo "Number of General Staff: $#"
echo "All parameters: $*"
echo "So parameters: [email protected]"
echo "The execution of the last command: $?"
echo "Current shell process id:$$"
SH var.sh a B C--executes the script and then appends the parameters
File name: var.sh
Parameter 1:a
Parameter 2:b
Number of General Staff: 3
All parameters: a b C
So the parameter: a b C
Execution of the last command: 0--0 represents the last command execution succeeded, non-zero indicates that the last command failed to execute
Current Shell Process id:4982
4.read command
The read command puts the input data received into a standard variable.
Echo-n "Enter your name:"--the function of parameter-n is not to wrap, echo by default is a newline
Read name
echo "Hello $name"
Read-p "Enter your name:" Name---parameter-p allows a prompt to be specified directly on the Read command line
echo "Hello $name"
Linux0 Foundation Depth Parsing Shell Introduction 01