Home>Article>Operation and Maintenance> Completely master Shell programming in Linux systems
This article brings you knowledge about shell programming in Linux systems. Before learning Shell programming, we should first know what Shell is and what shell scripts are. I hope it will be helpful to everyone.
## Shell is an application, which can also be said to be a command interpreter. It is a user The bridge between the Linux kernel and the Linux kernel 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 schedules various hardware and other resources to complete the user's operations.
What is the Linux kernel? In the Linux operating system, the part that can actually operate the computer hardware to complete a certain user function is called the kernel of the Linux system. When users use a Linux system, they cannot directly operate the kernel, but indirectly operate the kernel through the Shell. The Shell is not part of the kernel, but an application developed outside the Linux kernel. It passes the received user mouse click operations or input commands to the kernel, and the kernel then schedules the hardware to complete the specified operation. In this way, users do not need to directly operate the kernel, but indirectly operate the kernel through the Shell. The kernel will not be directly exposed to the outside, which ensures the security of the kernel and simplifies user operations.
SHELL This application is started at the boot. When we operate the Linux system, we are always directly or indirectly operated the Linux kernel through the shell. In fact, before there was a graphical interface, users directly called the Shell application through the terminal or console, and then operated the Linux system by entering commands. The $ and # that users see on the console or terminal are actually the Shell command prompt, which indicates that the user has entered the Shell program and only needs to enter commands to operate the Linux kernel through the Shell. It's just that $ is displayed when the root user logs in, and # is displayed when the ordinary user logs in.
In addition, the process of Shell transferring user operations 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. 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(1)
sh: The full name ofsh 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 C language, so it is named C shell, or csh for short.(3)
tcsh:tcsh is an enhanced version of csh, adding command completion function and providing more powerful syntax support.(4)
ash:A lightweight Shell that takes up less resources, suitable for running in low-memory environments, and fully compatible with the bash shell.
(5)bash:The bash shell was developed by the GNU organization and maintains compatibility with the sh shell. It is the default shell configured in various Linux distributions.
In the Linux system, the default Shell is generally the bash shell. Shell is an application program, which is usually placed in the/bin
or/user/bin
directory. The Shells available in the current Linux system are all recorded in/etc/shells
file.
(1) To view the currently available shells of the system, execute the command [cat -n /etc/shells]:
(2) To view the current default of the system To use the shell, execute the command [echo $SHELL]:
(3) To view the shell used by each user, execute the command [cat -n /etc/passwd]:
We already know that Shell is an application, and this application can not only pass user operation commands to the Linux kernel, it also supports programming. Shell will correspond to a set of programming language syntax, and this set of languages is called Shell programming language or Shell scripting language. The Shell programming language is a scripting language like the JavaScript language. It does not require compilation, and its interpreter is the Shell application itself.
The Shell we talk about in our daily work, in most cases, refers to the Shell script language, not the Shell application.
When we use the Shell script language, we can use it in conjunction with other operating commands of the Linux system (such as ls, grep, etc.), and we use the Shell script The language is combined with other commands to write a text with a .sh extension that can complete a specific function, which is called a Shell script.
We already know that there are many versions of Shell, and the Shell script language syntax supported by each version may be different. , and all the following examples of Shell scripts are for this version of Bash Shell.
(1) Create a new Hello.sh script in the root directory: [vim Hello.sh]
(2) Then enter the following content:
#!/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. The /bin/bash that follows is the path of the Shell, so [#!/bin/bash] indicates the declaration of use. The bash shell in the bin directory serves 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 completed.
Additional explanation:
We have already said above that the Shell program is the interpreter of the Shell script language, and when we use the terminal ( For example, connecting to the terminal of the Linux server through For example, declare a variable and print the value of the variable:
(1) Enter the 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 printing the value of the variable name.
## Of course, this method is only suitable for simple execution logic with only one or two sentences of code. In most cases, we still write the Shell programming code in a .sh script. Go and execute again.Above we have written a script that can output Hello World! The Shell script is run, and now we have to execute the Hello.sh script. There are two ways to execute a Shell script:
The first one is to give the .sh text executable permission, and then execute the text:
(1) Give the Shell first Script executable permissions: [chmod x
Hello.sh
];
(2) Direct execution: absolute path: [/Hello.sh] or relative path: [./Hello.sh [
注意 Attention, if you use the relative path when you implement it here, you must start with ./ to indicate the current directory. Otherwise to the script, causing execution to fail.
out out out out of Bash Shell is a program, and then pass the Hello.sh script as a parameter to Shell: [/bin/bash Hello.sh] or [bash Hello.sh]
(2) It should be noted here that when executing a Shell script in this way, there is no need to declare which version of Shell to use as the interpreter in the first line of the Shell script, that is, there is no need for this line of code [#!/ bin/bash], because the command we use to run the script [/bin/bash Hello.sh] has already specified which version of the Shell to use as the interpreter.
to be executed in this way. If a relative path is used, then [/bin/bash ./Hello.sh] and [/bin/bash Hello.sh] are the same and are not There is no difference in using the first execution method.Related recommendations: "Linux Video Tutorial
"The above is the detailed content of Completely master Shell programming in Linux systems. For more information, please follow other related articles on the PHP Chinese website!