[BASH] Review and knowledge points sorting out (1)

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

foreword

I don’t know if there are any friends who are like the author. I have read bash commands over and over again. When writing scripts, there are still some vague commands and unclear parameters. Reasons for analysis: First, when learning, I just simply checked the commands or grammar used to cope with the current work, but did not conduct overall sorting and build a complete knowledge system. The second point is forgetting (the author sometimes has a really bad memory). The reason for forgetting may be that the memory is not deep (the author can clearly remember the first time I went to kindergarten when I was a child, and I skipped class halfway through the class and went to the grass to eat delicious food. end home scene - -!).

Starting from this issue, the author will linux圣经reorganize his bash knowledge system based on chapters 10, 11, and 12 of "Bird Brother's Linux Private Kitchen" and some online courses of Ma Ge (Ma Yongliang) , and strive to lay a solid foundation. During the period, some master-written scripts encountered in work will also be cited as reference learning. Gossip less, let us start a new journey!

1. Understanding and learning BASH

It is actually the core (kernel) of the operating system that manages the entire computer hardware, and this core needs to be protected! Therefore, our general users can only communicate with the core through the shell, so that the core can achieve the work we want. So how many shells are available on the system? Why do we use bash?

1.1 Hardware, Core and Shell

This should be a pretty interesting topic: "What is a Shell"? I believe that as long as you have touched a computer, most of your friends who have a little idea of ​​the operating system (whether it is Linux, Unix or Windows) have heard of this term, because as long as there is an "operating system", the Shell is inseparable. However, before discussing the Shell, let's first understand how a computer works! For example: When you want your computer to transmit "music", what does your computer need?

  1. Hardware: Of course, your hardware needs to be equipped with a "sound card chip", otherwise there will be no sound;
  2. Core management: The core of the operating system can support this chipset, and of course the driver of the chip needs to be provided;
  3. Application: The user (that is, you) needs to input the command to generate the sound!

This is the basic steps needed to output sound! In other words, after you have to "input" a command, the "hardware" will work through the command you issued! So how does the hardware know the instructions you issued? That is the control job of the kernel (core)! In other words, we must communicate the commands we input with the Kernel through the "Shell", so that the Kernel can control the hardware to work correctly! Basically, we can illustrate it through the following picture:

insert image description here
The operating system is actually a set of software. Because this set of software controls the entire hardware and manages system activity monitoring, if this set of software can be operated by the user at will, if the user does not use it properly, the entire system will crash! Because the operating system manages the entire hardware function! So of course it cannot be used casually by some end users without management capabilities.

But we always need to allow users to operate the operating system, so there are applications developed on the operating system! Users can command the core through the application, so that the core can achieve the hardware tasks we need!
insert image description here
We can find that the application program is actually in the outermost layer, just like the shell of an egg, so this Dongdong is also called a shell program (shell)!

In fact, the function of the shell program is only to provide an interface of the user's operating system, so the shell program needs to be able to call other software. We can operate these applications through the shell program (that is, the command line mode), and let these applications call the core to perform the required work! Does this have a certain concept for the shell program?

That is to say, as long as the interface that can operate the application program can be called a shell program. In a narrow sense, the shell program refers to software in the command line, including bash, which will be introduced in this chapter. The generalized shell program includes software with a graphical interface! Because the graphical interface can actually operate various applications to call the core work! But in this chapter, we are mainly using bash!

1.2 Why learn the shell with text interface?

The shell of the text interface is very difficult to learn, but there are many benefits after learning it!
The tool for X Window and web interface, although its interface is friendly and its function is powerful, but after all, it is just a set of application programs that integrate all the software used, not a complete suite, so sometimes When you upgrade or use other package management modules (such as tarballs instead of rpm files, etc.), it will cause configuration troubles. Even the X window interfaces designed by different distributions are not the same, which also causes troubles in learning. The shell of the text interface is different! Almost all distributions use the same bash! In this way, you can easily switch between different distributions, just like the martial arts novel mentioned "one way, ten thousand ways!"

In addition, Linux management often needs to be connected remotely, and the transmission speed of the text interface must be faster when connected, and it is less prone to disconnection or information leakage. Therefore, the shell is really something to learn tool . Moreover, he can let Linux go deeper and understand him better, instead of just clicking the mouse! The so-called "God help those who help themselves! 』Touching a little more text-mode things will bring you closer to Linux!

1.3 Legal shell and /etc/shells function of the system

After knowing what a shell is, let's find out which shell Linux uses? What! which one? Isn't a shell just "a shell?" "Ha ha! That's not it! Due to the many developers in the early Unix era, there are many versions of the shell according to different developers , such as the commonly heard Bourne SHell (sh), the default C SHell in Sun, and the commonly used K SHell in business ,, and TCSH, etc., each Shell has its own characteristics. As for the version used by Linux, it is called "Bourne Again SHell (abbreviated as bash)". This shell is an enhanced version of Bourne Shell, and it is also developed based on the GNU architecture!

Before introducing the advantages of the shell, let's talk about the simple history of the shell:
the first popular shell was developed by Steven Bourne. In memory of him, it is called the Bourne shell, or simply abbreviated sh !
Later, another widely circulated shell was designed by Bill Joy of Berkeley University and attached to the shell of the BSD version of the Unix system. The syntax of this shell is somewhat similar to the C language, so it is named C shell, abbreviated as csh ! Since the Sun host is quite powerful in academia, and Sun is mainly one of the branches of BSD, the C shell is also one of the other very important and widely spread shells.

So how many shells can we use in our Linux (take CentOS 7.x as an example) at present? You can check the /etc/shells file, there are at least the following shells that can be used (Bird brother omitted the repeated shells! Including /bin/sh is equal to /usr/bin/sh!):

[root@node-135 ngx_on_cpu]# cat /etc/shells
/bin/sh (已经被 /bin/bash 所取代)
/bin/bash (就是 Linux 预设的 shell)
/bin/tcsh (整合 C Shell ,提供更多的功能)
/bin/csh (已经被 /bin/tcsh 所取代)
/usr/bin/sh
/usr/bin/bash

Different versions of centos may have slightly different shells

Although the functions of various shells are similar, they are different in the delivery of some syntax, so it is recommended that you still have to choose a certain shell to get familiar with it. Linux comes with bash by default, so just learning bash at first is pretty awesome! _ ! Also, hey! Why should the legal shell on our system be written into the /etc/shells file? This is because some services of the system will check the shells that users can use during operation, and the query of these shells is through the file /etc/shells!

When can I as a user get a shell to work with? Also, which shell will my user get by default? Remember the login action we mentioned in the section Logging in to linux on the terminal interface in Chapter 4? When I log in, the system will give me a shell for me to work. And the shell obtained by this login is recorded in /etc/passwd this file! What is the content of this file?

[root@node-135 ngx_on_cpu]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
...

As shown above, the last data in each line is the default shell you can get after logging in! Then you will also see that the root is /bin/bash, but the system account bin and daemon, etc., use the weird /sbin/nologin, and more instructions will be provided later

BASH stands for "Bourne Again Shell", and it was developed by Brian Fox of the FSF. BASH is the compatible and open source successor to the Bourne shell. The Bourne shell is the shell used on the seventh edition of Unix. Known for its simplicity, compactness, and high school, written by AT&T. The pun of Bourne Again is born again, which means new life. Hacker humor.

1.4 Features of the Bash shell

Since /bin/bash is the default shell of Linux, it is always necessary to understand this thing! Bash is one of the important tool software in the GNU project, and is currently the standard shell of Linux distributions. bash Mainly compatible with sh, and enhanced shell version according to some user needs. No matter which distribution you use, you cannot escape the fate of needing to learn bash! So what are the benefits of this shell, why does Linux use him as the default shell? The main advantages of bash are as follows:

  • Command editing ability (history)
  • Command and file completion: (benefits of the [tab] key)
  • Command alias setting function: (alias)
  • Job control, foreground background control: (job control, foreground, background)
  • Programmatic scripts: (shell scripts)
  • Wildcard: (Wildcard)

Introduction to wildcards: In addition to complete strings, bash also supports many wildcards to help users query and issue commands. For example, want to know how many files starting with X are under /usr/bin? Use: "ls -l /usr/bin/X*" to know~ In addition, there are other wildcards available, which can speed up the user's operation!

1.5 Check whether the command is a built-in command of the Bash shell: type

Go to the online help file about Linux, that is, the content of the man page, so is there any documentation for bash? Just kidding~ How could such a great thing not have a documentation! Please enter directly in the shell environment and man bashtake a look, hehe! It's not covered! Let you read a bash documentation that you can't read through for a few days and nights, but it is very detailed data! _

However, in this bash man page, I don't know if you have noticed it, hey! Why is there other documentation in this documentation? For example, the description of the cd command is in this man page? Then when I typed man cd directly, why did a bunch of instruction introductions appear at the top of the screen that appeared? How is this going? In order to facilitate the operation of the shell, in fact, bash has "built-in" many commands, such as the cd mentioned above, and commands such as umask, etc., are all built-in in bash!

Then how do I know whether this command comes from an external command (referring to other commands not provided by bash) or is it built into bash? hey-hey! Use type this command to observe! for example:

type [-tpa] name
选项与参数:
   :不加任何选项与参数时,type 会显示出 name 是外部指令还是 bash 内建指令
-t :当加入 -t 参数时,type 会将 name 以底下这些字眼显示出他的意义:
	 file :表示为外部指令;
	 alias :表示该指令为命令别名所设定的名称;
	 builtin :表示该指令为 bash 内建的指令功能;
-p :如果后面接的 name 为外部指令时,才会显示完整文件名;
-a :会由 PATH 变量定义的路径中,将所有含 name 的指令都列出来,包含 alias


[root@node-135 /]# type -a java
java is /usr/bin/java
java is /opt/jdk-11.0.19/bin/java
[root@node-135 /]# type -t java
file
[root@node-135 /]# type -t cd
builtin
[root@node-135 /]# type -p java
/usr/bin/java
[root@node-135 /]# type java
java is /usr/bin/java
[root@node-135 /]# type ls
ls is aliased to `ls --color=auto'
[root@node-135 /]# type cd
cd is a shell builtin

Through the type command, we can know whether each command is a bash built-in command. In addition, when using type to search for the following name, if the following name cannot be found in the state of the executable file, then the name will not be displayed. In other words, type is mainly to find the "executive file" instead of the general file name! hehe! Therefore, this type can also be used as a function similar to which command! Find instructions!

1.6 Command release and quick edit button

If the instruction string is too long, how to use two lines to output?

[dmtsai@study ~]$ cp /var/spool/mail/root /etc/crontab \
> /etc/fstab /root

The purpose of the above command is to copy the three files to the /root directory. However, because the command is too long, Brother Bird uses " \[Enter] " to press the [Enter] button to "escape! Come on, let the [Enter] button no longer have the function of "Start Execution"! So that the command can continue to be entered on the next line. Special attention should be paid to the [Enter] key followed by a backslash (), and there are no other characters in between. Because \ only escapes the "immediately next character"! Therefore, if I write: " \ [Enter]", that is, when there is a space between [Enter] and the backslash, then \ will escape the "space bar" instead of the [Enter] button! Please take a closer look at this place! Very important!

If you successfully escape [Enter], the symbol > will appear at the top of the next line, and you can continue to enter commands! In other words, it >appears automatically by the system, and you don't need to enter it.

When the command you need to issue is very long, or you have entered a series of wrong commands, you want to quickly delete the entire string of commands. Generally speaking, we press the delete button. Are there any other quick key combinations that can help? There is! The common ones are as follows:

Key combination Function and Demonstration
[ctrl]+u/[ctrl]+k Respectively delete the instruction string from the cursor forward ([ctrl]+u)and delete the instruction string backward ([ctrl]+k)
[ctrl]+a/[ctrl]+e Respectively let the cursor move to the front ([ctrl]+a)or the end of the entire command string ([ctrl]+e)

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

Guess you like

Origin blog.csdn.net/u010230019/article/details/132016610
Recommended