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

What are the basics of Shell scripting

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the basics of Shell scripting". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn the basics of Shell scripting.

Shell itself is a program written in C language, and it is a bridge for users to use Linux. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes commands entered by users; as a programming language, it defines a variety of variables and parameters, and provides many control structures that are unique in high-level languages, including loops and branches.

Although it is not a part of the core of the Linux system, it invokes most of the functions of the system core to execute programs, establish files and coordinate the operation of each program in a parallel way. Therefore, for users, shell is the most important utility, in-depth understanding and proficiency in the characteristics of shell and how to use it is the key to make good use of Linux system.

It can be said that the proficiency in the use of shell reflects the user's proficiency in using Linux.

Shell has two ways to execute commands:

Interactive (Interactive): interprets and executes the user's command, the user enters a command, Shell interprets and executes one.

Batch: the user writes a Shell script (Script) in advance, in which there are many commands that Shell can execute at a time without having to type them one by one.

Shell scripts are very similar to programming languages, with variables and flow control statements, but Shell scripts are interpreted and executed without compilation. Shell programs read and execute these commands from the script line by line, which is equivalent to a user typing commands in the script line by line to the Shell prompt for execution.

For beginners of Shell, please note that in normal applications, it is recommended that you do not run Shell under your root account. As an ordinary user, you can't break the system intentionally or unintentionally, but if it's root, it's different, and just typing a few letters can lead to catastrophic consequences.

Several common Shell

As mentioned above, Shell is a scripting language, so you must have an interpreter to execute these scripts.

The common Shell script interpreters on Linux are bash, sh, ash, csh, and ksh, which are traditionally called a Shell. We often talk about how many Shell there are, but we are actually talking about the Shell script interpreter.

Bash

Bash is the default shell used by the Linux system. Bash, done jointly by Brian Fox and Chet Ramey, is an acronym for BourneAgain Shell, with a total of 40 internal commands.

Linux uses it as the default shell because it has features such as:

You can use functions similar to doskey under DOS to look up and quickly enter and modify commands with arrow keys.

Automatically gives a command that starts with a string by looking for a match.

Contains its own help function, you only need to type help at the prompt to get related help.

Sh

Sh is developed by Steve Bourne and is an abbreviation for Bourne Shell. Various UNIX systems are equipped with sh.

Ash

Ash shell is written by Kenneth Almquist, and Linux is a small shell that takes up the least system resources. It contains only 24 internal commands, so it is very inconvenient to use.

Csh

Csh is the larger kernel of Linux. It is compiled by 47 authors represented by William 喜悦 and has 52 internal commands. The shell actually points to a shell like / bin/tcsh, that is, csh is actually tcsh.

Ksh

Ksh is an acronym for Korn shell and is written by Eric Gisin with 42 internal commands. The biggest advantage of this shell is that it is almost fully compatible with the commercial version of ksh, so you can try the performance of the commercial version without paying for the commercial version.

Differences between Shell and compiled languages

Generally speaking, programming languages can be divided into two categories: compiled languages and interpreted languages.

Compiled language

Many traditional programming languages, such as Fortran, Ada, Pascal, C, C++ and Java, are compiled. Such languages need to convert our written source code (source code) into object code (object code) in advance, a process known as "compilation".

When running the program, read the object code (object code) directly. Because the compiled object code (object code) is very close to the bottom of the computer, so the execution efficiency is very high, which is the advantage of compiled language.

However, because compiled languages mostly operate at the bottom, dealing with bytes, integers, floating-point numbers, or other machine-level objects, it often requires a lot of complex code to implement a simple function. In C++, for example, it is difficult to copy all the files in one directory to another.

Interpretive language

Interpretive languages are also known as "scripting languages". When executing such programs, the interpreter (interpreter) needs to read the source code (source code) we have written, convert it into object code (object code), and then run it by the computer. Because there is more compilation process each time the program is executed, the efficiency is reduced.

The advantage of using scripting languages is that they mostly run at a higher level than compiled languages and can easily handle objects such as files and directories; the disadvantage is that they are generally not as efficient as compiled languages. On balance, however, scripting is usually worth it: a simple script that takes an hour to write, the same function implemented in C or C++, can take two days, and in general, the script executes fast enough to ignore its performance problems. Examples of scripting languages are awk, Perl, Python, Ruby, and Shell.

When to use Shell

Because Shell seems to be a common function among UNIX systems and has been standardized by POSIX. Therefore, Shell scripts can be applied to many systems as long as they are "written by heart" once. Therefore, the reason to use Shell scripts is based on:

Simplicity: Shell is a high-level language through which you can succinctly express complex operations.

Portability: using the functions defined by POSIX, scripts can be executed on different systems without modification.

Easy to develop: you can complete a powerful and useful script in a short time.

However, given the command limitations and efficiency issues of Shell scripts, Shell is generally not used in the following situations:

1. Resource-intensive tasks, especially when efficiency needs to be considered (for example, sorting, hash, etc.).

two。 Mathematical operations that require dealing with large tasks, especially floating-point operations, precise operations, or complex arithmetic operations (this case usually uses C++ or FORTRAN).

3. There is a need for cross-platform (operating system) migration (usually using C or Java).

4. Complex applications, when structured programming must be used (requires type checking of variables, function prototypes, etc.).

5. The application of critical tasks that affect the overall situation of the system.

6. Tasks with high security requirements, such as you need a robust system to prevent intrusion, cracking, malicious sabotage, and so on.

7. The project consists of a series of dependent parts.

8. Large-scale file operations are required.

9. Multidimensional array support is required.

10. Requires the support of data structures, such as linked lists or numbers.

11. Need to generate or operate graphical interface GUI.

twelve。 Direct operating system hardware is required.

13. Either the Icano or socket interface is required.

14. You need to use the interface of the library or legacy old code.

15. Private, closed-source applications (shell scripts put the code in a text file that can be seen all over the world).

If your application fits any of the above, consider a more powerful language-- perhaps Perl, Tcl, Python, Ruby--, or a higher-level compiled language such as Cmax Clippers, or Java. Even so, you will find that using shell to prototype your application is very useful in the development steps.

The first Shell script

Open the text editor and create a new file with the extension sh (sh stands for shell). The extension does not affect the execution of the script. If you use php to write shell scripts, use php as the extension.

Enter some code:

The code is as follows:

#! / bin/bash

Echo "Hello World!"

"#!" Is a convention tag that tells the system what interpreter the script needs to execute, no matter which Shell is used. The echo command is used to output text to the window.

There are two ways to run Shell scripts.

As an executable program

Save the above code as test.sh and cd to the appropriate directory:

The code is as follows:

Chmod + x. / test.sh # gives the script execution permission

. / test.sh # execute script

Note that it must be written as. / test.sh, not test.sh. Run other binary programs are the same, directly write test.sh,linux system will go to PATH to find whether there is called test.sh, and only / bin,/ sbin, / usr/bin,/usr/sbin and so on in PATH, your current directory is not usually in PATH, so write as test.sh will not find the command, to use. / test.sh to tell the system, just look in the current directory.

To run the bash script in this way, the first line must be written correctly so that the system can find the correct interpreter.

The "system" here is actually the application shell (imagine Windows Explorer), but I deliberately wrote it into a system, which is easy to understand. Since this system refers to shell, can a script using / bin/sh as an interpreter omit the first line? Right.

As an interpreter parameter

This mode of operation is to run the interpreter directly, whose argument is the file name of the shell script, such as:

The code is as follows:

/ bin/sh test.sh

/ bin/php test.php

Scripts run in this way do not need to specify interpreter information on the first line, and it is useless to write them.

At this point, I believe you have a deeper understanding of "what are the basics of Shell scripting?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report