Shell programming of Linux system

1. What is Shell

        1. Before learning Shell programming, we should know what Shell is

        The relationship between users, Shell, Linux kernel, and hardware is as follows:

        Shell is an application program, or a command interpreter. It is a bridge between the user and the Linux kernel. It can pass the user's operations on the graphical interface or the commands entered in the terminal to the Linux kernel, and then the Linux kernel Schedule various hardware and other resources to complete user operations.

        What is the Linux kernel? In the Linux operating system, the part that can actually operate the computer hardware to complete a user function is called the kernel of the Linux system. When users use the Linux system, they cannot directly operate the kernel, but indirectly operate the kernel through the Shell. Shell is not a part of the kernel, but an application developed outside the Linux kernel. It passes the received user mouse clicks or input commands to the kernel, and the kernel then schedules the hardware to complete the specified operations. In this way, the user does not need to directly operate the kernel, but indirectly operates the kernel through the shell, and the kernel will not be directly exposed to the outside, which ensures the security of the kernel and simplifies the user's operation.

        Shell is an application that starts at boot. When we operate the Linux system, we operate the Linux kernel directly or indirectly through the Shell all the time. In fact, before there is no graphical interface, the user directly calls the Shell application through the terminal or console, and then operates the Linux system by entering commands. The $ and # that the user sees on the console or terminal are actually the command prompt of the Shell, which indicates that the user has entered the Shell program, and only needs to enter the command to operate the Linux kernel through the Shell. It's just that $ is displayed for root user login, and # is displayed for ordinary user login.

        In addition, the process of the Shell passing the user's operation to the kernel is the process of calling the API interface provided by the kernel. For example, the user performs an operation of opening a file on the graphical interface or terminal command line, and after receiving the user's operation, the Shell will call the corresponding function provided by the kernel, and then the kernel will schedule hardware resources to complete the user's operation. operate.

        2. Common Shell

        We know that Linux is an open source operating system, it is jointly developed by multiple organizations or individuals around the world, each organization or individual is responsible for a part of the function, and finally combined, it constitutes the current we use Linux. It is also for this reason that these different organizations or individuals will develop applications that can be used in Linux systems, and the functions of these applications may be similar, and each has its own advantages and disadvantages. As for which one to use, the user chooses. And Shell is such an application, so there are many versions of Shell, and the default Shell used by most Linux distributions is bash shell. Other common shell versions are as follows:

        (1) sh: The full name of sh is Bourne shell, which is the standard shell on UNIX. Many UNIX versions are equipped with sh. sh was the first popular shell.

        (2) csh: The syntax of this shell is somewhat similar to the C language, so it is named C shell, abbreviated as csh.

        (3) tcsh: tcsh is an enhanced version of csh, which adds command completion and provides more powerful syntax support.

        (4) ash: A lightweight shell that occupies less resources, suitable for running in a low-memory environment, and fully compatible with the bash shell.

        (5) bash: The bash shell is developed by the GNU organization and maintains compatibility with the sh shell. It is the default configuration shell of various Linux distributions.

        3. View the Shell of the Linux system

       In Linux systems, the default shell is generally the bash shell. Shell is an application program, which is generally placed in /binor /user/bindirectory, and the shells available in the current Linux system are recorded in /etc/shellsfiles.

(1) Check the shells currently available in the system and execute the command [cat -n /etc/shells]:

(2) To view the shell currently used by the system by default, execute the command [echo $SHELL]:

 

(3) Check the Shell used by each user and execute the command [cat -n /etc/passwd]:

2. What is Shell programming

        1. What is the Shell programming language

        We already know that the Shell is an application, and this application not only can pass the user's operation command to the Linux kernel, it also supports programming. Shell corresponds to the syntax of a programming language, and this language is called Shell programming language or Shell scripting language. The Shell programming language, like the JavaScript language, is a scripting language that does not require compilation, and its interpreter is the Shell application itself.

        The Shell we usually talk about in our work, in most cases, refers to the Shell scripting language, not the Shell application.

       2. What is Shell Script 

        When we use the Shell scripting language, we can use it in combination with other operating commands of the Linux system (such as ls, grep, etc.), and we use the Shell scripting language and other commands to write an extension that can complete a specific function. text is called a shell script.

        3. The first shell script

        We already know that there are many versions of Shell, and the shell scripting language syntax supported by each version may be different, and all the following examples of Shell scripts are for the Bash Shell version.

(1) Create a new Hello.sh script in the root directory: [vim Hello.sh]

(2) Then enter the following:

#!/bin/bash     
echo "Hello World "

        The first line: #! is a convention mark, which can tell the system which shell version to use as the interpreter for this script, and the following /bin/bash is the path of the shell, so [#!/bin/bash ] Indicates that the bash shell in the bin directory is used as the interpreter for this script;

        The second line: [echo "Hello World"] means the output text Hello World! ;

(3) Then save and exit: [!wq];

(4) In this way, one can output Hello World! The script is written.

Additional instructions:

       As we have said above, the Shell program is the interpreter of the Shell scripting language, and when we use the terminal (such as the terminal connected to the Linux server through Xshell), we have actually entered the Shell program, so in fact, we can directly use the terminal. Enter the code of the Shell programming language to run, instead of writing it in a script to run it. For example, declare a variable and print the variable's value:

        (1) Input code: [name=Zhang San], which means that a variable name is defined, and its value is Zhang San;

        (2) Enter the code: [echo $name], which means to print the value of the variable name.

        Of course, this method is only suitable for situations where the execution logic is simple with only one or two lines of code. In most cases, we still write the Shell programming code in the .sh script and then execute it.

         4. How to run a shell script

        Above we have written a program that outputs Hello World! The Shell script runs, and now we are going to execute the Hello.sh script. There are two ways to execute shell scripts:

         The first: give the .sh text executable permission, and then execute the text:

        (1) First give the shell script executable permission: [ chmod +x Hello. sh];

        (2) Direct execution: absolute path: [/Hello.sh] or relative path: [./Hello.sh]

                Note that if a relative path is used when executing here, it must start with ./ to indicate the current directory, otherwise the system will not find the script and the execution will fail.

         Second: pass the shell script as a parameter to the Bash shell interpreter:

        (1) Here Bash Shell is used as the interpreter, then we directly call the Bash Shell program, and then pass the Hello.sh script as a parameter to the Shell: [/bin/bash Hello.sh] or [bash Hello.sh] sh]

        (2) It should be noted here that when executing the shell script in this way, there is no need to declare which version of the shell to use as the interpreter in the first line of the shell script, that is, this line of code is not required [#!/bin /bash], because [/bin/bash Hello.sh] in the command we run the script has already specified which version of Shell is used as the interpreter.

                Note that when executing the script file in this way, if a relative path is used, then [/bin/bash ./Hello.sh] is the same as [/bin/bash Hello.sh], and the first execution is not used. difference in method.

Guess you like

Origin blog.csdn.net/weixin_55118477/article/details/121361619