Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Shell programming in Linux system

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of Shell programming in Linux system, which is very detailed and has certain reference value. Friends who are interested must finish it!

one。 What is Shell1. Before we learn Shell programming, we should know what Shell is.

The relationship between user, Shell, Linux kernel and hardware is shown below:

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

What is the kernel of Linux? In the Linux operating system, the part that can really operate the computer hardware to complete a user function is called the kernel of the Linux system. When using Linux system, users can not directly operate the kernel, but indirectly operate the kernel through Shell. Shell is not a part of the kernel, but an application developed outside the Linux kernel. It passes the commands received by the user with a mouse click or input to the kernel, and the kernel schedules the hardware to complete the specified operation. In this way, users do not need to operate the kernel directly, but operate the kernel indirectly through Shell, and the kernel will not be directly exposed to the outside, which ensures the security of the kernel and simplifies the operation of users.

The Shell application starts at boot time. When we operate the Linux system, we operate the Linux kernel directly or indirectly through Shell all the time. In fact, before there is no graphical interface, users directly call the Shell application through the terminal or console, and then operate the Linux system by entering commands. The $and # seen by the user on the console or terminal are actually the command prompt for Shell, which indicates that the user has entered the Shell program and can operate the Linux kernel through Shell by entering the command. It's just that root user login displays $, while normal user login displays #.

In addition, the process of 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 does a file opening operation in the graphical interface or terminal command line, and after the user's operation is received by the Shell, it will call the corresponding function provided by the kernel, and then the kernel will schedule the hardware resources to complete the user's operation.

two。 Common Shell

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

(1) the full name of sh:sh is Bourne shell, which is the standard shell on UNIX. Many versions of UNIX come with sh. Sh is the first popular Shell.

(2) csh: the syntax of this shell is somewhat similar to the C language, so it is named C shell, or csh for short.

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

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

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

3. View the Shell of the Linux system

In Linux systems, the general default Shell is bash shell. Shell is an application that is typically placed in the / bin or / user/bin directory, while the Shell available to the current Linux system is recorded in the / etc/shells file.

(1) View the Shell currently available on the system, and execute the command [cat-n / etc/shells]:

(2) View the shell currently used by the system by default and execute the command [echo $SHELL]:

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

two。 What is Shell programming 1. What is the Shell programming language

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

When we talk about Shell in our daily work, in most cases, we refer to the Shell scripting language, not Shell applications.

two。 What is a Shell script

When we use the Shell scripting 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 scripting language and other commands to write text with a .sh extension that can accomplish a particular function, which is called a Shell script program.

3. The first Shell script

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

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

(2) then enter the following:

#! / bin/bash echo "Hello World"

The first line: #! Is a convention tag that tells the system which Shell version of the script to use as the interpreter, followed by / bin/bash is the path to the Shell, so [#! / bin/bash] declares that the bash shell in the bin directory is used as the interpreter for this script

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

(3) then save and exit: [! wq]

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

Supplementary note:

As we have said above, the Shell program is the interpreter of the Shell scripting language, and when we use the terminal (for example, the terminal connecting to the Linux server through Xshell), we have already entered the Shell program, so in fact, we can directly enter the Shell programming language code on the terminal to run, rather than writing in the script to run. For example, declare a variable and print the value of the variable:

(1) input code: [name= Zhang San], indicating that a variable name is defined, whose value is Zhang San.

(2) enter the code: [echo $name] to indicate the value of the print variable name.

Of course, this approach is only suitable for situations where the logic is simple and there are only one or two sentences of code, and in most cases we still write the Shell programming code in a .sh script before executing it.

4. The way to run Shell scripts

Above we have written a can output Hello World! The Shell script runs, and now we are going to execute the Hello.sh script. Shell scripts can be executed in two ways:

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

(1) first give 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 for execution here, it must start with. /, indicating the current directory, otherwise the system will not find the script, resulting in execution failure.

The second: pass the Shell script as a parameter to the Bash Shell interpreter:

(1) Bash Shell is used as the interpreter here, so we can directly call the program Bash Shell, and then pass the script Hello.sh as an argument to Shell: [/ bin/bash Hello.sh] or [bash Hello.sh]

(2) it should be noted that when executing a Shell script in this way, you do not need to declare in the first line of the Shell script which version of Shell to use as the interpreter, that is, you do not need this line of code [#! / bin/bash], because we have already indicated which version of Shell is used as the interpreter in the command [/ bin/bash Hello.sh] of the script.

Note that when executing script files in this way, [/ bin/bash. / Hello.sh] and [/ bin/bash Hello.sh] are the same if you use a relative path, without using the difference in the first mode of execution.

The above is all the content of the article "sample Analysis of Shell programming in Linux system". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report