[BASH] Review and knowledge points sorting (7)

This series of catalogs --> [BASH] review and knowledge points sorting (catalog)

7. Summary and practice of knowledge points in the first six chapters

7.1 Summary

  • Since the core is a protected block in memory, we must communicate the commands we input with the Kernel through the "Shell", so that the Kernel can control the hardware to work correctly. Users cannot directly operate the kernel, and there are only two ways to operate the kernel:
    1. Call the system call interface
    2. glibc encapsulates library functions for users, and users indirectly call the system call interface through glibc
      insert image description here
  • The main reasons for learning the shell are: the shell of the text interface is the same in all major distributions; the speed of the text interface is faster in remote management; the shell is a very important part of managing the Linux system, because many controls in Linux are written in the shell.
  • The legal shells of the system are all written in the /etc/shells file;
    [root@node-135 ~]# cat /etc/shells
    /bin/sh
    /bin/bash
    /usr/bin/sh
    /usr/bin/bash
    
  • The shell obtained by the user's default login is recorded in the last field of /etc/passwd;
  • The functions of bash mainly include: command editing ability; command and file completion function; command alias setting function; job control, foreground background control; programmed script; wildcard
  • type can be used to find out what type of execution command is, and can also be used for the same function as which; main parameters:-p -t -a
  • A variable is a set of text or symbols, etc., to replace some settings or a string of reserved data
  • Variables mainly include environment variables and custom variables, or global variables and local variables
  • Use env and export to observe environment variables, where export can convert custom variables into environment variables; declare/typeset -xthe usage and function are the same as export
  • set can observe all variables in the current bash environment; unset cancels variables
  • $? is also a variable, which is the returned value after the execution of the previous command. in Linux 回传值为 0 代表执行成功;
  • locale can be used to observe language information;
  • Read can be used to allow the user to input the value of the variable from the keyboard; the main parameters:-p -t
  • ulimit can be used to limit the user's use of system resources; common parameters:-u -f -n
  • The configuration file of bash is mainly divided into login shell and non-login shell. The login shell mainly reads /etc/profile and ~/.bash_profile, while the non-login shell only reads ~/.bashrc
  • When using vim, if you accidentally press [crtl]+s, the screen will be frozen. You can use [ctrl]+q to unfreeze
  • Wildcards mainly include: *, ?, []etc.
  • Data flow redirection transfers the output information to other files or devices through symbols such as >, 2>, <;
  • The issuance of continuous commands can be handled by symbols such as ; && ||
  • The key point of the pipeline command is: "The pipeline command will only process the standard output, and the standard error output will be ignored." "The pipeline command must be able to accept the data from the previous command as the standard input to continue processing."
  • Pipeline commands mainly include: cut, grep, sort, wc, uniq, tee, tr, col, join, paste, expand, split, xargs etc. Although some commands are not pipeline commands, they can still be used in combination with pipeline commands, for examplels

7.2 Exercises

  1. Scenario simulation question 1: Since ~/.bash_history can only record commands, I want to record the time every time I log out, and record 50 subsequent commands. How can I do this?
    o Goal: understand history, and record historical commands through data flow redirection;
    o Prerequisite: You need to understand the data flow redirection in this chapter and understand the information of bash's various environment configuration files.
    Answer: In fact, the processing method is very simple. We can understand that date can output time, and use ~/.myhistory to record all historical records. Currently, the latest 50 historical records can be displayed using history 50, so we can modify ~/.myhistory bash_logout becomes the following:
[dmtsai@study ~]$ vim ~/.bash_logout
date >> ~/.myhistory
history 50 >> ~/.myhistory
  1. What shells (name three) can you find on Linux? That file records available shells? And what is the default shell of Linux?
		bash
		sh
		csh
		/etc/shells
		bash
  1. After you input a series of instructions, you find that the long list of data written earlier is wrong, and you want to delete the contents of the instruction string from the cursor position to the front, what should you do?
    ctrl + u
    ctrl + k to delete backward
    ctrl + a to move to the beginning of the line
    ctrl + e to move to the end of the line
  2. In the shell environment, there is a prompt character (prompt), can it be modified? What to change? What is the default prompt character content?
    Yes
    Environment variable PS1
    $, 『[\u@\h \W]$』==> ["The account name of the current user"@"Only take the name of the host name before the first decimal point" "The name of the current working directory "]$
  3. How to display the HOME environment variable?
[root@node-135 ~]# echo $HOME
/root
  1. How to know the setting value of all current variables and environment variables?
    Environment variables: env, export
    All variables: decaler, set
[root@node-135 ~]# abcde=1234
[root@node-135 ~]# env|grep abcde
[root@node-135 ~]# export|grep abcde
[root@node-135 ~]# declare|grep abcde
abcde=1234
[root@node-135 ~]# set|grep abcde
abcde=1234
  1. Can I set a variable named 3myhome?
    No, the variable name consists of numbers, letters and underscores, and the first character cannot be a number
  2. In such an exercise "A=B" and "B=C", if I issue "unset $A", is the variable to be canceled A or B?
    B
    unset $A is equivalent to unset B
  3. How to cancel the contents of variables and command aliases?
    unset unalias
  4. How to set a variable name as name and content as It's my name?
    name="It's my name"
  5. What are the two main types of reading of bash environment configuration files? Which important files are read separately?
    login-shell/no-login-shell
    login-shell reads /etc/profile and ~/.bash_profile
    no-login-shell reads ~/.bashrc
  6. CentOS 7.x man page path configuration file?
    /etc/man_db.conf
  7. Try explaining ', ", and 这些符号在变量定义中的用途? 双单引号':标记字符串,字符串内容不支持转义 双双引号":标记字符串,字符串内容支持转义 双撇\: used to execute the commands in it, same as $()
  8. What is the escape symbol \ used for?
  • Can be escaped in echo -e string
    [root@node-135 ~]# echo -e "123\t123">123
    [root@node-135 ~]# cat 123
    123     123
    
  • become a general character
    [root@node-135 ~]# echo "\"it is cat"\"
    "it is cat"
    [root@node-135 ~]# echo "it is cat"
    it is cat
    
  • Escape symbols, if the command is too long, it can be wrapped
  1. What is the difference between ;, &&, || in consecutive commands?
    ;: Commands are executed in sequence, cmd1; cmd2, whether the previous command cmd1 is successful or not, cmd2 will execute
    &&: cmd1 && cmd2, cmd1 is executed successfully, cmd2 is executed. cmd1 failed to execute, and cmd2 was not executed.
    ||: cmd1 || cmd2, cmd1 is executed successfully, cmd2 is not executed. cmd1 failed to execute, execute cmd2.
  2. How to separate the account number from the last result, and print out the account number that has been logged in?
last|cut -d' ' -f 1
last|cut -d' ' -f 1|sort |uniq
  1. May I ask foo1 && foo2 | foo3 > foo4, in this command string, is foo1/foo2/foo3/foo4 a command or a file? What is the meaning of the whole string of instructions?
    foo1, 2, 3 instructions, foo4 file
    foo1 fails to execute, the program ends,
    foo1 executes successfully, foo2 executes successfully, stdout is output to foo3 as stdin, foo3 is executed, and the result of foo3 execution is output to foo4 foo1 executes successfully
    , foo2 executes Failed, output empty output to foo3 as stdin, execute foo3, the result of foo3 execution is output to foo4, there is nothing in foo4
  2. How to show details of any file name starting with a under /bin?
[root@node-135 ~]# ls /bin/a* -l
  1. How to show the files under /bin with four-character filenames?
ls -ld /bin/????
  1. How to show the files under /bin whose file name does not start with ad?
ll /bin/[^a-d]*
  1. I want to change the login prompt character of the terminal interface to my own preference, where should I change it? (filename)
[root@node-135 ~]# cat /etc/issue
\S (terminal: \l)
Date: \d \t
Kernel \r on an \m
Welcome!
  1. Continuing from the previous question, if I want to display the welcome message after the user logs in, where should I change it?
[root@node-135 ~]# cat /etc/motd
Hello everyone,
Our server will be maintained at 2015/07/10 0:00 ~ 24:00.
Please don't login server at that time. ^_^

This series of catalogs --> [BASH] review and knowledge points sorting (catalog)

Guess you like

Origin blog.csdn.net/u010230019/article/details/132064362