Table of contents
Linux history, tab, alias, command execution sequence, pipe character and exit
Modify the default number of recorded historical commands
Case 1 --- Display the top 10 highest occurrences in history records
Case 2 --- Add time information displayed in history
Command and file name completion --- tab
If the previous command is not executed successfully, the following command will not be executed.
Linux history, tab, alias, command execution sequence, pipe character and exit
history history command
Format
history [parameter] [history command save file]
parameter
parameter | effect |
-c | Clear history command |
-w | Write the historical commands in the cache to the historical command save file. If you do not manually specify the history command save file, put it into the default history command save file ~/.bash_history |
Modify the default number of recorded historical commands
[root@localhost ~]# vim /etc/profile
Case
Case 1 --- Display the top 10 highest occurrences in history records
[root@localhost ~]# history | tr -s " " | cut -d " " -f3 | sort | uniq -c |sort -nr | head -10
浏览命令 | 压缩为1个空格 | 截取以空格为分割的第3部分 | 排序 | 统计并去重 | 降序数字排序| 显示前10个
Case 2 --- Add time information displayed in history
[root@localhost ~]# vim ~/.bashrc # 定位最后一行增加一行内容
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S: "
[root@localhost ~]# source ~/.bashrc # 刷新配置
[root@localhost ~]# history
Command and file name completion --- tab
command alias
Format
alias alias=original command
Case
[root@localhost ~]# alias hi=history
[root@localhost ~]# hi
Notice
Aliases have higher priority than commands
第一顺位:执行用绝对路径或相对路径执行的命令。
第二顺位:执行别名。
第三顺位:执行 Bash 的内部命令。
第四顺位:执行按照 $PATH 环境变量定义的目录查找顺序找到的第一个命令。
In order to make this alias permanent, you can write the alias into the environment variable configuration file "~/.bashrc
[root@localhost ~]# vim ~/.bashrc
# 在最下面增加
Command execution order
sequential execution
command 1; command 2
[root@localhost ~]# date ; ls -l /etc/passwd
If the previous command is not executed successfully, the following command will not be executed.
Command 1 && Command 2
[root@localhost ~]# mkdir /mnt/iso && mount /dev/sr0 /mnt/iso
If the previous command is successful, it will not be executed later. If the previous command is unsuccessful, it will be executed later.
Command 1 || Command 2
[root@localhost ~]# mkdir tt || ls /
[root@localhost ~]# mkdir tt || ls / # 可以再次执行
pipe character
When a pipe is set between two commands, the output of the command on the left of the pipe symbol | becomes the input of the command on the right. As long as the first command writes to standard output and the second command reads from standard input, the two commands can form a pipe
symbol
Command 1 | Command 2
Case
Extract the IP address of the system network card
[root@localhost ~]# ip a | grep ens160 | grep inet | tr -s " " | cut -d / -f1 | cut -d " " -f3
显示IP信息 | 行向过滤包含ens60 | 行向过滤inet | 压缩为一个空格 | 以/为分割依据,取第一部分 | 以空格为分割依据,取第3部分
Display the remaining memory capacity of the host
[root@localhost ~]# free -h |grep Mem | tr -s " " | cut -d " " -f4
exit exit program
effect
Terminate the execution of the shell program
Format
exit status code
status code
This parameter is an integer value, its value range is 0~255
Notice:
The exit status code of the Shell program is stored in the system variable $?. Therefore, the user can obtain the exit status code returned by the Shell program to the parent process through this variable.
Common status codes
0----------------命令运行成功
1----------------通知未知错误
2----------------误用shell命令
126-------------命令不可执行
127-------------没有找到命令
128-------------无效退出参数
128+x-----------linux信号x的严重错误
130--------------命令通过Ctrl+C终止
255--------------退出状态码越界
Case
Demonstrates that the program returns different status codes under different circumstances
[root@localhost ~]# echo "china"
[root@localhost ~]# echo $?
[root@localhost ~]# ehco "china"
bash: ehco: command not found...
Similar command is: 'echo'
[root@localhost ~]# echo $?