Experimental environment
centos8.2 pstree
yum install -y psmisc
Shell script is a dynamic weakly typed language
Variable definition and reference
Types of | For example |
---|---|
Ordinary variable | Only the current shell takes effect, its shell subprocess and other shells are not effective |
Environment variable | The current shell and its shell child processes take effect |
Local variable | A piece of code in the current shell process, usually a function |
Environment variable
- The child process inherits the environment variables of the parent process, and the environment variables of the child process cannot be used by the parent process
- Once the child process modifies the environment variable of the parent process, the new value will be passed to the grandchild process
- Generally used in system configuration files
Variable declaration and assignment
- export name=value
- declare -x name=value
Variable reference
$name, ${name}
Display environment variables
env, printenv, export, declare -x
Delete the environment variable
ueset name
Location variable
Location variables: built-in variables in the bash shell, call the parameters passed to the script through the command line in the script code
parameter | effect |
---|---|
$1,$2…${10}… | Corresponding to the first few parameters |
$0 | The command itself, including the path |
$* | All parameters passed to the script, all parameters are combined into a string |
$@ | All parameters passed to the script, each parameter is a separate string |
$# | Number of parameters passed to the script |
For example:
[root@centos8 scripts ]#bash -n systeminfo.sh
[root@centos8 scripts ]#bash -x systeminfo.sh
+ RED='\e[1;31m'
+ GREEN='\e[1;32m'
+ END='\e[0m'
+ echo -e '\e[1;32m-------------Host systeminfo-----------------\e[0m'
-------------Host systeminfo-----------------
++ hostname
+ echo -e 'HOSTNAME: \e[1;31mcentos8.magedu.org\e[0m'
HOSTNAME: centos8.magedu.org
++ ifconfig
++ grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'
++ head -n1
+ echo -e 'IPADDR: \e[1;31m192.168.20.128\e[0m'
IPADDR: 192.168.20.128
++ cat /etc/redhat-release
+ echo -e 'OVERSION: \e[1;31mCentOS Linux release 8.2.2004 (Core) \e[0m'
OVERSION: CentOS Linux release 8.2.2004 (Core)
++ uname -r
+ echo -e 'KERNAL: \e[1;31m4.18.0-193.el8.x86_64\e[0m'
KERNAL: 4.18.0-193.el8.x86_64
++ cut -d : -f2
++ lscpu
++ grep 'Model name'
++ tr -s ' '
+ echo -e 'CPU: \e[1;31m Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz\e[0m'
CPU: Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz
++ free -h
++ grep Mem
++ cut -d : -f2
++ tr -s ' ' :
+ echo -e 'MEMORY: \e[1;31m952Mi\e[0m'
MEMORY: 952Mi
++ lsblk
++ grep '^sd'
++ tr -s ' '
++ cut -d ' ' -f4
+ echo -e 'DIEK: \e[1;31m200G\e[0m'
DIEK: 200G
+ echo -e '\e[1;32m---------------------------------------------\e[0m'
---------------------------------------------
[root@centos8 scripts ]#bash systeminfo.sh
-------------Host systeminfo-----------------
HOSTNAME: centos8.magedu.org
IPADDR: 192.168.20.128
OVERSION: CentOS Linux release 8.2.2004 (Core)
KERNAL: 4.18.0-193.el8.x86_64
CPU: Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz
MEMORY: 952Mi
DIEK: 200G
---------------------------------------------