What is Shell, Shell command two ways of execution

Recently, the new project has started. It is necessary to add a new model to the original product type. This is similar to adding support for a product in the Linux kernel. We know that if we want to understand a product, we must look at his Makefile. A large amount of work, usually write the execution of the makefile in the script file, today we come together to review the shell script related knowledge.

First, Shell Introduction: What is the Shell, Shell command two ways of execution

Shell itself is a program written in C language. It is a bridge for users to use Unix/Linux. Most of the user's work is done through the Shell. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes user-entered commands; as a programming language, it defines various variables and parameters, and provides many control structures that are available in high-level languages, including loops and branches. Although it is not part of the Unix/Linux system kernel, it calls most of the system's core functions to execute programs, create files, and coordinate the execution of individual programs in parallel. Therefore, for the user, the shell is the most important utility. In-depth understanding and proficiency in the use of the shell's features is the key to using the Unix/Linux system. It can be said that the proficiency of shell use reflects the user's proficiency in using Unix/Linux. Note: It is not meaningful to learn shells separately. Please understand Unix/Linux basics first. Shell has two ways to execute commands:

• Interactive: interprets the user's command, the user enters a command, and the shell interprets the execution.

· Batch: The user writes a shell script in advance. There are a number of commands that allow the shell to execute these commands at once without having to type commands one by one.

Shell script and programming language are very similar, there are variable and process control statements, but the shell script is interpreted and does not need to be compiled, the shell program reads and executes these commands line by line from the script, which is equivalent to a user's command in the script Proceed to the shell prompt line by line. Shell beginners Please note that in normal applications it is not recommended to run the shell with the root account. As an ordinary user, you cannot destroy the system, whether you intend it or not, but if it is root, it is different. Just typing a few letters can cause catastrophic results.

Second, several common shell

As mentioned above, Shell is a scripting language, then there must be an interpreter to execute these scripts. The common shell script interpreters on Unix/Linux are bash, sh, csh, ksh, etc. They are customarily referred to as a shell. We often say how many shells there are, in fact, shell script interpreters.

Bash

Bash is the default shell for Linux standards. This tutorial is also based on bash. Bash was completed by Brian Fox and Chet Ramey. It is an abbreviation for BourneAgain Shell. There are a total of 40 internal commands. Linux uses it as the default shell because it has features such as the following:

· You can use functions like doskey under DOS, use the arrow keys to view and quickly enter and modify commands.

Automatically finds commands that begin with a string by looking for matches.

· Contains its own help function, you can get help by typing help at the prompt.

Sh

Sh was developed by Steve Bourne and is an abbreviation for Bourne Shell. sh is the default shell of the Unix standard.

Ash

The ash shell was written by Kenneth Almquist. Linux uses a small shell with minimal system resources. It contains only 24 internal commands and is therefore inconvenient to use.

Csh

Csh is a relatively large kernel for Linux. It is composed of a total of 47 authors represented by William Joy and has 52 internal commands. The shell is actually a shell such as /bin/tcsh, that is, csh is actually tcsh.

Ksh

Ksh is an abbreviation of Korn shell, written by Eric Gisin, and has 42 internal commands. The biggest advantage of this shell is that it is almost completely compatible with the commercial distribution of ksh, so that you can try commercial versions without spending money on commercial versions. Note: bash stands for Bourne Again Shell and is the default shell for the Linux standard. It is based on the Bourne shell and absorbs some of the features of the C shell and the Korn shell. Bash is fully compatible with sh, that is, scripts written in sh can be executed without bash in bash.

Third, the difference between Shell script language and compiled language

In general, 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 languages. Such languages ​​need to convert our written source code into object code in advance. This process is called "compiling." When the program is run, the object code is read directly. Since the compiled object code is very close to the computer's bottom layer, execution is very efficient, which is the advantage of compiled language. However, because compiled languages ​​mostly operate at the bottom level and deal with bytes, integers, floating-point numbers, or other machine-level objects, implementing a simple function requires a lot of complex code. For example, in C++, it is difficult to perform simple operations such as "copying all files in one directory to another."

Interpreted language

Explained languages ​​are also called "scripting languages." When executing such a program, the interpreter needs to read the source code we wrote and convert it into object code, which is then run by the computer. Because every time the program is executed, the compilation process is much more efficient. The advantage of using a scripting programming language 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 often less efficient than compiled languages. But under the trade-off, it is usually worthwhile to use scripting: it takes an hour to write a simple script. The same function is written in C or C++. It may take two days. In general, the script execution speed is fast enough. Is fast enough to let people ignore its performance problems. Examples of scripting languages ​​are awk, Perl, Python, Ruby, and Shell.

Fourth, when to use the shell

Because the 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 "intentionally" once. Therefore, the reason why you want to use a shell script is based on:

· Simplicity: The Shell is a high-level language; through it, 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: A powerful and useful script can be completed in a short time.

However, taking into account the shell script's command restrictions and efficiency issues, the following circumstances generally do not use the shell:

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

2. Mathematic operations that need to handle large tasks, especially floating-point operations, precise operations, or complex arithmetic operations (this is typically handled using C++ or FORTRAN).

3. There are cross-platform (operating system) migration requirements (generally using C or Java).

4. Complex applications, when it is necessary to use structured programming (needs for type checking of variables, function prototypes, etc.).

5. For mission-critical applications that affect system globality.

6. For missions with high security requirements, for example you need a robust system to prevent intrusions, cracks, vandalism, etc.

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

8. Large-scale file operations are required.

9. Need multidimensional array support.

10. Need data structure support, such as linked list or number data structure.

11. Need to create or operate graphical interface GUI.

12. Need direct operating system hardware.

13. I/O or socket interfaces are required.

14. Need to use libraries or legacy legacy code interfaces.

15. Private, closed-source applications (shell scripts put code in text files that the world can see).

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

Fifth, the first shell script

Open a text editor and create a new file with the extension sh (sh stands for shell). The extension does not affect the script execution. If you use php to write a shell script, the extension will use php. Enter some code:

00001. #!/bin/bash

00002. echo "Hello World !"

"#!" is a convention tag that tells the system what interpreter the script needs to execute, which shell to use. The echo command is used to output text to the window. There are two ways to run a shell script.

As an executable program

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

Chmod +x ./test.sh # Make the script executable

./test.sh #execute script

Note that you must write ./test.sh instead of test.sh. The same is true for running other binary programs. Write test.sh directly. The linux system will go to the PATH to find out if it is called test.sh. Only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in the PATH. Your current directory is usually not in PATH, so writing test.sh will not find the command. Use ./test.sh to tell the system to find it in the current directory. To run the bash script in this way, the first line must be written correctly so that the system finds the correct interpreter. The "system" here is actually the shell application (Imagine Windows Explorer), but I deliberately wrote it as a system. It is easy to understand. Since the system is a shell, then a script that uses /bin/sh as an interpreter is not Can you save the first line? Yes.

As an interpreter parameter

This way of operating is to run the interpreter directly. Its parameter is the file name of the shell script, such as:

/bin/sh test.sh

/bin/php test.php

Scripts that run this way do not need to specify interpreter information on the first line, and it is useless to write. Look at another example. The following script uses the read command to get input from stdin and assign it to the PERSON variable, and finally outputs it on stdout:

[cpp] view plain copy

1. # Script follows here:

2. echo "What is your name?"

3. read PERSON

4. echo "Hello, $PERSON"

Run the script:

Chmod +x ./test.sh

$./test.sh

What is your name?

Mozhiyan

Hello, mozhiyan

$

WiFi 5 Ceiling Wireless AP

Wifi 5 Ceiling Wireless Ap,Wifi Access Point Poe Ceiling Mount,Ceiling Mount Wifi Access Point Home,Access Point Enclosure Ceiling

Shenzhen MovingComm Technology Co., Ltd. , https://www.movingcommiot.com