Linux——Shell script programming(1)

1. Why should you learn Shell programming?

1) When Linux operation and maintenance engineers perform server cluster management, they need to write Shell programs for server management.
2) For JavaEE and Python programmers, work needs require you to write some Shell scripts for program or server maintenance, such as writing a script to regularly back up the database.
3) For big data programmers, they need to write Shell programs to manage the cluster.

2. Shell usage scenarios

①. What is Shell? 

        Due to reasons such as security, complexity, and cumbersomeness, users cannot directly access the kernel (and there is no need). Another program needs to be developed to allow users to use this program directly; the function of this program is to receive user operations (click icons, enter commands) and perform simple Processed and then passed to the kernel, so that users can indirectly use the operating system kernel. Adding a layer of "agent" between the user and the kernel can not only simplify operations but also ensure kernel security.


        This separately developed program is this layer of "agent". This command line program under Linux is called  Shell . It is an application that allows users to use the Linux kernel more efficiently, safely, and at low cost. This is the essence of Shell . The Shell itself is not part of the kernel. It is just an application written on the basis of the kernel. It is no different from other software such as QQ, Thunder, Firefox, etc. However, Shell also has its uniqueness, which is that it starts immediately after booting ; users use Linux through Shell, and users cannot use Linux without starting Shell.

②. How does Shell connect the user and the kernel?

        The functions of the Shell program itself are very weak. For example, file operations, input and output, process management, etc. all rely on the kernel. When you run a command, in most cases the Shell will call the interface exposed by the kernel, which is using the kernel. However, this process is hidden by the Shell, and it is carried out silently behind the scenes and cannot be seen.
        The interface is actually a function, and using the kernel is to call these functions. Is this all there is to using the kernel? um, yes! There is no other way to use the kernel except functions. cat log.txtFor example , you can view the contents of the log.txt file by entering a command in the Shell . However, where is log.txt placed on the disk? How many data blocks is it divided into? Where to start? Where does it terminate? How to operate the probe to read it? The Shell does not know any of these underlying details. It can only call the open() and read() functions provided by the kernel, telling the kernel that I want to read the log.txt file, and then the kernel will read the file according to the instructions of the Shell. And the read file content is handed over to the Shell, and finally the Shell presents it to the user (in fact, the presentation on the monitor depends on the kernel). The entire process Shell is a "middleman" that "resells" data between the user and the kernel.

3. Shell connects to other programs

Some of the commands entered in the Shell are built-in commands         that come with the Shell itself ; some are from other applications (a program is a command), which are called external commands. The commands supported by the Shell itself are not multifunctional and have limited functions, but the Shell can call other programs, and each program is a command, so that the number of Shell commands can be infinitely expanded. The result is that the Shell function is very powerful and fully capable of daily management of Linux, such as Text or string retrieval, file search or creation, large-scale software automatic deployment, changing system settings, monitoring server performance, sending alarm emails, crawling web content, compressing files, etc. What's even more surprising is that Shell can also connect multiple external programs and easily transfer data between them, that is, passing the output of one program to another program as input. Shell is powerful not because Shell itself is rich in functions, but because it is good at using and organizing other programs. The position of Shell in the entire Linux system can be described as shown in the figure below. Note that "Users" and "Other Applications" are connected by a dotted line, because the user directly faces the Shell after starting Linux, and other applications can be run through the Shell.

4. Shell programming (Shell is a scripting language)

        Shell is not a simple stack of commands. We can also program in Shell, which   is no different from using common programming languages ​​such as C++ , C# , Java , and Python . Although Shell is not as powerful as C++, Java, Python, etc., it still supports basic programming elements, such as:

  • if...else selection structure, case...in switch statement, for, while, until loop;
  • Concepts such as variables, arrays, strings, comments, addition, subtraction, multiplication and division, logical operations, etc.;
  • Functions, including user-defined functions and built-in functions (such as printf, export, eval, etc.).

        From this perspective, Shell is also a programming language, and its compiler (interpreter) is the Shell program. What we usually call Shell sometimes refers to the program that connects the user and the kernel, and sometimes it refers to Shell programming. Shell is mainly used to develop some practical and automated gadgets, rather than developing medium and large-scale software with complex business logic, such as detecting computer hardware parameters, building a Web operating environment, log analysis, etc. Shell is very suitable.
        Especially for Linux operation and maintenance engineers, Shell is indispensable and a skill that must be mastered. It allows us to automatically manage server clusters. Otherwise, you have to log in to all servers one by one and perform operations on each server. There may be hundreds or thousands of servers with the same setup, which wastes a lot of time on repetitive tasks.

5. Common Shells

Common Shells include sh, bash, csh, tcsh, ash, etc.

sh

        The full name of sh is Bourne shell, which was developed by Steve Bourne of AT&T Company. In order to commemorate him, it was named after him. sh is the standard shell on UNIX, and many UNIX versions come with sh. sh was the first popular shell.

csh

        Another widely circulated shell after sh was designed by Bill Joy of Berkeley University. The syntax of this shell is somewhat similar to the C language, so it is named C shell, or csh for short. Bill Joy is a man of great influence. He founded the BSD operating system, developed the vi editor, and was one of the founders of Sun.

BSD is an important branch of UNIX, on which later generations have developed many modern operating systems, the most famous of which are FreeBSD, OpenBSD and NetBSD. Even Mac OS X is largely based on BSD.

tcsh

        tcsh is an enhanced version of csh, adding command completion function and providing more powerful syntax support.

ash

        A simple lightweight shell that takes up few resources and is suitable for running in low-memory environments, but is fully compatible with the bash shell mentioned below.

bash

        The bash shell is the default shell in Linux. Developed by the GNU organization, bash maintains compatibility with the sh shell and is the default shell configured in various Linux distributions.

Bash compatibility with sh means that Shell code written for sh can run in bash without modification.

Still, there are some differences between bash and sh:

  • On the one hand, bash extends some commands and parameters;
  • On the other hand, bash is not completely compatible with sh. Some of their behaviors are inconsistent, but in most enterprise operation and maintenance cases, the difference is not big. In special scenarios, bash can be used instead of sh.

6. How to execute Shell script


Script format requirements

1) The script starts with #!/bin/bash
2) The script needs to have executable permissions


Method 1 (enter the absolute path or relative path of the script)

Note: First, give +x permission to the helloworld.sh script, and then execute the script

For example, /hello.sh or use the absolute path /root/shcode/hello.sh

Method 2 (sh+script)

Note: There is no need to grant +x permission to the script, just execute it directly. For example, shhello.sh, you can also use absolute paths


7. Write the first Shell script

Create a Shell script to output hello world!

vim hello.sh
#!/bin/bash
echo "hello,world~"

 8. Reference materials: Shell Script Manual.pdf (aliyuncs.com)

Guess you like

Origin blog.csdn.net/weixin_49171365/article/details/131505612