Variable
-
Get the native host name
#hostname
-
Get the system version number
#uname-R
-
Get CPU Type
-
Get the CPU working frequency
Cat/proc/cpuinfo
-
Get Memory usage
#free-M
-
Get use of hard disk
#df
Variable type and variable operation
-
Local variables (generally referred to)
The local variable exists only in the user's current shell, and when the user exits a new shell of the current shell or opener, the variable set will not exist
Setting variables
#变量名 = variable Value
Show variables
#echo ${Variable Name}
Suppose a=1 shows the Echo ${a}
-
Environment variables (very few)
Environment variables are used for all user processes (often referred to as child processes). The logon process is called the parent process. The user processes executed in the shell are called child processes. Unlike local variables, environment variables are available for all child processes, including editors, scripts, and applications
Environment variables disappear when the user logs off, so it is best to define them directly in the $home/.bash_profile (/etc/profile) file
-
Positional variables (very much related to scripts and functions)
Position variable indicates $0,$1 ... $ (only 9)
$ |
$ |
$ |
$ |
$ |
$ |
$ |
$ |
$ |
$ |
Script Name |
A |
B |
C |
D |
E |
F |
G |
H |
I |
$0= Current script Name
To use positional parameters in a script
Passing parameters to system commands
-
Special variables
$# the number of argument lists passed to the script
$* ($@) displays all parameters passed to the script in a single string
$$ the current process ID number of the script run
$? Displays the exit status of the last command. 0 indicates no errors, and any other value indicates an error.
The need for reference
In a variable operation, the easiest mistake to make when a script executes a variable assignment is to refer to the error
Example: Echo ABC *
Echo ABC ' * '
echo Hit the star button to exit *
Way of referencing
double quotation marks (") (you need to exclude the following several)
Use double quotes to refer to any character or string except $, ', \, character
single quotation mark (') (more overbearing, anything can be converted to characters)
Similar to double quotes, the shell ignores any reference values.
In other words, if the special meaning is masked, all the characters in the quotation marks will be used as a string.
Inverted quotation mark (') (to be executed as a command in parentheses)
Used to set the output of a system command to a variable.
The shell takes the contents of the inverted quotes as a system command and executes its contents
Back slash (\)
If a character has a special meaning, the backslash prevents the shell from misinterpreting its meaning, that is, masking its special meaning.
The following characters contain a special meaning:& * + ^ $ ' "|?
Script instance
-
hello.sh
#!/bin/bash
#This is the
echo "hello,world!"
Perform:
chmod u+x hello.sh
./hello.sh
-
var.sh
#!/bin/bash
echo $
echo $
echo $
echo $
echo $
echo $
echo $
echo $
echo $
echo $
-
Echo–e "Hostname\t ' hostname '"
Echo–e "Oscore\t ' uname-r '"
Echo–e "Cpuinfo\t ' grep" model name "/proc/cpuinfo|awk–f: ' {print $} '"
Ehco–e "Cpumhz\t ' grep" MHz/proc/cpuinfo | Awk–f: ' {print $} '
Echo–e "Memtotal\t ' free | awk ' $1= ' Mem: ' {print $} ' '
Echo–e "Diskinfo\t ' df | grep Dev |awk ' {print $} '
Review:
Structure and operation of scripts
Types and usage of variables
Environment variables
Local variables
Parameter variable
Special variables
Reference method when assigning a variable value