Variable
- Get local host name
# Hostname
- Obtain the System Version Number
# Uname-r
- Obtain CPU type
- Obtain the cpu operation frequency
Cat/proc/cpuinfo
- Obtain memory usage
# Free-m
- Obtain hard disk usage
# Df
Variable types and variable operations
- Local variable (generally referred)
The local variable only exists in the user's current shell. When the user exits the current shell or opens a new shell, the set variable will not exist.
Set Variables
# Variable name = variable value
Show Variables
# Echo $ {variable name}
If a = 1 is displayed, echo $ {}
- Environment variables (rarely used)
Environment variables are used for all user processes (often referred to as subprocesses ). A logon process is called a parent process. User processes executed in Shell are called sub-processes. Unlike local variables, environment variables can be used for all sub-processes, including editors, scripts, and applications.
Environment Variables disappear when the user logs out, so it is best to define them directly in the $ HOME/. bash_profile (/etc/profile) file.
- Location variable (closely related to scripts and functions)
The position variable indicates $0, $1... ... $9 (only 9)
$0 |
$1 |
$2 |
$3 |
$4 |
$5 |
$6 |
$7 |
$8 |
$9 |
Script Name |
A |
B |
C |
D |
E |
F |
G |
H |
I |
$0 = Current Script Name
Use location parameters to scripts
PASS Parameters to system commands
- Special Variables
$ # Number of parameters passed to the script list
$ * ($ @) Displays all parameters passed to the script in a single string
$ ID of the current process running the script
$? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.
Necessity of reference
In variable operations, the most common mistake when the script executes variable assignment is a reference error.
Example: echo abc *
Echo abc '*'
Echo Hit the star button to exit *
Reference Method
Double quotation marks (") (The following must be excluded)
Double quotation marks can be used to reference any character or string except $, ', \, and character.
Single quotes (') (overbearing, any character can be converted)
Similar to double quotation marks, shell ignores any referenced values.
In other words, if the special meaning is blocked, All characters in the quotation marks are treated as a string.
Back quotes (') (execute the brackets as commands)
Used to set the output of system commands to variables.
Shell uses the content in the back quotes as a system command and executes its content
Backslash (\)
If a character has a special meaning, the backslash prevents shell from misunderstanding its meaning, that is, shielding its special meaning.
The following characters have special meanings: & * + ^ $ '"|?
Script instance
- Hello. sh
#! /Bin/bash
# This is the first script
Echo "Hello, World !"
Run:
Chmod u + x Hello. sh
./Hello. sh
- Var. sh
#! /Bin/bash
Echo $1
Echo $2
Echo $3
Echo $4
Echo $5
Echo $6
Echo $7
Echo $8
Echo $9
Echo $10
- #! /Bin/bash
Echo-e "hostname \ t 'hostname '"
Echo-e "OScore \ t 'uname-R '"
Echo-e "CPUInfo \ t 'grep" model name "/proc/cpuinfo | awk-F: '{print $2 }''"
Ehco-e "CPUMHz \ t' grep" MHz "/proc/cpuinfo | awk-F: '{print $2 }''"
Echo-e "MEMTotal \ t' free | awk '$1 =" Mem: "{print $2 }''"
Echo-e "DiskInfo \ t 'df | grep dev | awk '{print $1 $2 }''"
Review:
Script structure and running
Variable type and usage
Environment Variable
Local variable
Parameter variables
Special Variables
Reference Method for variable assignment