Shell several key explanations
#! /bin/bash 脚本解释器,特定约定
# 注释
$a 值替换,`if [-n "$a"]` 替换变量a的值到字符串中进行比较
'' 单引号,字符串限定,内部的内容不进行解析,只当作普通字符串
"" 双引号,内部的内容不能识别命令,只进行变量的解析`$a`
`` 反斜单引号(倒引号),内部命令会当作shell指令执行,然后组合其他字符组成最后的字符串。`test=`date +%F`; echo "Tody $test";`
\ 转义字符,与C类似
{} 大括号,与$配合来连接字符串。`$a; ${a};` 一般是等价的。
() 小括号,命令组,命令替换,初始化数组
[] 中括号,一般用来进行流控,`if []; then fi`
; 分号,同一行的不同语句,与C类似。
= 变量定义,等号两边不能有空格。`a="abc"`
由于变量使用弱类型,所以`a=xyz; a='xyz'; a="xyz";`表达的结果一致。如果式数字,不使用引号
但是有单引号双引号等的区别,建议一般情况时使用双引号,防止出现意外的结果。
变量赋值时,不用加引号`a="123"; b=a;`
变量获取`v1=$(pwd); v2=`pwd`; `
unset v1; 删除变量
$0 当前脚本文件名
$n(n>=1) 传入脚本/函数的第n个参数
$# 传入的参数个数
$* 以一个单字符串显示所有向脚本传递的参数
$@ 与$*类似,一般加双引号, 但是它将每个参数单独显示,而$*把所有参数当作一个显示
$? 显示最后命令的退出状态或者函数的返回值,0表示没有错误
$$ 脚本运行的当前进程号
${#a} 获取字符串a的长度
${a:1:2} 字符串a第二哥开始,长度为2的子字符串
a=("1" "3" "a") 定义一个数组,空格为分隔符
${a[1]} / ${a[@]} / ${a[*]} 获取数组的元素
${#a[@]} 获取数组长度
:<<EOF
这里是注释内容
EOF也可以是其他字符,比如!
EOF
`read -p "please input a number to var1:" var1`读取控制台输入到变量
`[$a == $b]`条件运算符,方括号,并且中间必须使用空格,支持`+,-,*,/,%,=,==,!=`
`-a` 与运算,`-o` 或运算, `!` 非运算,`&&` and运算, `||` or运算
`[$a -eq $b] / test $a $b -eq` 一般性的比较判断,支持`-eq, -ne, -gt, -lt, -ge, -le`
`[-z str]` 字符串为空 true, `[-n str]`字符串不为空true
`a=$((a1*a2)); a=`expr a1 + a2`;` 计算表达式
`-d filename` 目录并且存在,
`-f filename`文件并且存在,
`-e filename`文件是否存在
`-r filename`文件是否可读
`-w filename`文件是否可写
`-x filename`文件是否可执行
`-b filename`文件是否块设备
`-c filename`文件是否字符设备
关于逻辑判断的符号:
[ ] : 中括号旁边和运算符两边必须添加空格 (可以使用,不推荐)
[[ ]]: 中括号旁边和运算符两边必须添加空格 (字符串验证时,推荐使用)
(()) : 中括号旁边和运算符两边必须添加空格 (数字验证时,推荐使用)
## 以下都支持 break和continue
if []; then
# case
elif
# case
else
# case
fi
case $a in
1)
# case 1
;;
2)
# case 2
;;
*)
# default case
;;
esac
for v1 in {1..5}; do
# 如果使用$@,$*作为items, 命令加上双引号,效果式不同的
echo v1
done
while $a; do
# case
done
# 函数声明
function name (){
action;
[return int;] #可加可不加
}
environment variable
There are three variables in the shell:
- Local variables, defined and used in scripts
- Shell variables, special variables set by the shell program itself
- System environment variables are provided by the operating system and are loaded into shell variables when the shell starts.
Divided by the life cycle of variables :
- Permanent environment variable: Configured in the environment variable script file, the variable takes effect permanently, and these scripts will be executed automatically every time the user logs in.
- Temporary environment variable: temporarily defined in the shell,
export
declared with the command, the variable becomes invalid when the shell is closed.
Divided according to the valid scope of the variable :
- System environment variables: public, valid for all users.
- User environment variable: private to the user, only valid for that user.
Some commonly used environment variables:
PATH: The search directory for executable programs, which determines which directories the shell will look for commands or programs
HOME: The home directory of the current user
USER: The username of the currently logged in user
HISTSIZE: The number of historical commands saved
LOGNAME: The login name of the current user
HOSTNAME : refers to the name of the host
SHELL: the current user Shell parser
LANG: the language, region, and character set of the Linux system
LANGUGE: language-related environment variables, which can be modified for multiple languages
In Linux, the files related to environment variables are as follows:
- global configuration
- /etc/profile
global configuration, all users will read this file when they log in.
Do you reopen the terminal after logging in as a user? This/etc/profile
will not be executed again. It means that the variables temporarily added in this file after login need to be restarted to take effect or executed manuallysource /etc/profile
. - /ect/bashrc
global configuration, all users will read this file when they log in, the environment variables configured in this file will affect the bash shell used by all users, no matter how bash is executed, this file will be read.
Ubuntu does not have this file, and the corresponding one is /ect/bash.bashrc.
After logging in as a user, do you reopen the terminal? This/ect/bash.bashrc
will still be executed. It is executed once when the terminal is opened, which means that the variables temporarily added in this file after login will take effect immediately when the terminal is reopened. - /etc/profile.d directory
/etc/profile will execute all script files under /etc/profile.d every time it starts. - user configuration
- ~/.profile
Executed when the current user logs in. If bash is executed in login mode, read it~/.bash_profile
. If it does not exist, read it~/.bash_login
. If the first two do not exist, read it~/.profile
.
Also, when logging in graphical mode, this file will be read even if it exists~/.bash_profile
and~/.bash_login
.
Do you reopen the terminal after logging in as a user? This~/.profile
will not be executed again. It/etc/profile
is similar to the above understanding. - ~/.bash_login
If bash is executed in login mode, read it~/.bash_profile
; if it does not exist, read it~/.bash_login
; if the first two do not exist, read it~/.profile
. - ~/.bash_profile
is executed when the user logs in, and each user can use this file to configure their own environment variables.
This file is only read when bash is executed as login. Usually the configuration file is also configured to be read~/.bashrc
.
Ubuntu does not have this file. - ~/.bashrc
This file is read when a user logs in and every time a new shell is opened.
This file is read when bash is executed in non-login mode. If executed as login, this file will not be read.
Do you reopen the terminal after logging in as a user? This~/.bashrc
will still be executed. It is executed once when the terminal is opened, which is/ect/bashrc
similar to the above understanding. - ~/.bash_logout
This file is executed every time you exit the system (exit the bash shell).
This file will only be read when logging out and in the form of longin. That is to say, this file will be read when logging out in text mode, and will not be read when logging out in graphic mode.
shell execution order
According to different login modes, the execution sequence of Shell is also different, generally as follows:
- Graphical mode login:
/etc/profile
—>~/.profile
- After logging in in graphical mode, when the terminal is opened, the reading sequence:
/etc/bash.bashrc
—>~/.bashrc
- When logging in in text mode:
/etc/bash.bashrc
—>/etc/profile
—>~/.bash_profile
If~/.bash_profile
the file does not exist, read it~/.bash_login
; if the first two do not exist, read it~/.profile
❤️ Combined with our above file program, we can get that the execution sequence of the Shell script on the virtual machine Ubuntu I use is:
/etc/profile
—>/etc/bashrc
—>/etc/profile.d
—>~/.profile
—>~/.bashrc
dpkg tool
The dpkg tool, the underlying package management tool of Ubuntu, is mainly used to manage the deb packages that have been downloaded and installed locally.
This tool just needs to remember its use, and its common commands are as follows:
安装软件: dpkg -i xxxx.deb
查看安装目录:dpkg -L xxxx #使用此命令不需要加.deb后缀,下面没有后缀的相同
显示版本:dpkg -l xxxx
查找包的详细信息:dpkg -s xxxx
列出deb包的内容:dpkg -c xxxx.deb
卸载软件(保留配置):dpkg -r xxxx
卸载软件(不保留配置):dpkg -P xxxx
解开 deb 包的内容 :dpkg –unpack xxxx.deb
配置包:dpkg –configure xxxx
apt is a new version of the package management tool, which solves the problem that the apt-get command is too scattered.
In simple terms: apt = collection of the most commonly used command options in apt-get, apt-cache, and apt-config.
Apt can be regarded as a subset of apt-get and apt-cache commands, which can provide necessary command options for package management.
The apt-get command is the same as the dpkg above, just remember to use it. His common commands are as follows:
更新源文件,并不会做任何安装升级操作:apt-get update
升级所有已安装的包 :apt-get upgrade
安装指定的包:apt-get install packagename
仅升级指定的包:apt-get install packagename --only-upgrade
重新安装包:apt-get install packagename --reinstall
修复安装:apt-get -f install
安装相关的编译环境:apt-get build-dep packagename
下载该包的源代码:apt-get source packagename
升级系统:apt-get dist-upgrade
使用 dselect 升级:apt-get dselect-upgrade
删除包(不删除配置文件) :apt-get remove packagename
删除包(删除配置文件):apt-get remove packagename -- purge
清理无用的包:apt-get clean
清理无用的包:apt-get autoclean
检查是否有损坏的依赖:apt-get check
查询指定的包 :apt-cache search packagename
显示包的相关信息:apt-cache show packagename
使用该包依赖哪些包:apt-cache depends packagename
查看该包被哪些包依赖:apt-cache rdepends packagename
disk management
`df -h -T` 输出硬盘使用状况和类型
`du -h --max-depth=1` 查看文件夹下文件夹大小
`lsblk` 列出所有磁盘情况
`fdisk -l` 打印硬盘硬件详细信息
`cat /proc/partitions` 查看磁盘分区blocks