A detailed introduction to the basics of shell scripting

Shell translates into a shell, which is wrapped in the outer layer of the Linux kernel , a human-machine interface that can issue relevant instructions to the operating system through a series of Linux commands . The shell can combine a series of linux commands through its conditional statements and loop statements to form a process-oriented program, shell script, to achieve some more complex functions.
To sum up, the shell is the general name of the linux command set, which is a man-machine interface belonging to the command line.

 

Shell itself is a program written in C language, which 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 the user; as a programming language, it defines various variables and parameters, and provides many control structures that are only available in high-level languages, including loops and branches.

Although it is not part of the Linux system core, it calls most of the system core functions to execute programs, create files and coordinate the execution of various programs in a parallel manner. Therefore, for users, the shell is the most important utility program. In-depth understanding and mastery of the characteristics and usage of the shell are the keys to making good use of the Linux system.

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

Shell has two ways to execute commands:

Interactive (Interactive): Interpret and execute the user's command, the user enters a command, and the Shell interprets and executes one.
Batch (Batch): The user writes a Shell script (Script) in advance, which has many commands, so that the Shell executes these commands at a time without having to type commands one by one.

Shell scripts are very similar to programming languages. They also have variables and flow control statements, but shell scripts are interpreted and executed without compilation. The shell program reads and executes these commands line by line from the script, which is equivalent to a user putting the commands in the script. Type line by line to the Shell prompt to execute.

Shell beginners, please note that in normal applications, it is recommended that you do not use the root account to run the Shell. As a normal user, you can't break the system, whether you intentionally or not; but if you're root, it's different, and just typing a few letters can lead to catastrophic consequences.

Several common shells

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

Common shell script interpreters on Linux include bash, sh, ash, csh, and ksh, which are customarily called a kind of shell. We often talk about how many kinds of shells there are, but we are actually talking about shell script interpreters.

bash

Bash is the default shell used by Linux systems. Bash is jointly completed by Brian Fox and Chet Ramey. It is the abbreviation of BourneAgain Shell, and there are a total of 40 internal commands.

Linux uses it as the default shell because it has features such as:
• You can use the functions similar to doskey under DOS, and use the arrow keys to view and quickly enter and modify commands.
• Automatically give commands starting with a string by looking for a match.
• Contains its own help function, you only need to type help below the prompt to get related help.

sh

sh Developed by Steve Bourne, it is the abbreviation of Bourne Shell, and various UNIX systems are equipped with sh.

ash

The ash shell is written by Kenneth Almquist, a small shell that occupies the least system resources in Linux. It only contains 24 internal commands, so it is very inconvenient to use.

csh

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

ksh

ksh is an acronym for Korn shell, written by Eric Gisin, and has a total of 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 the performance of the commercial version without paying for the commercial version.

Differences between Shells and Compiled Languages

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 ​​require pre-conversion of our written source code (source code) into object code (object code), a process called "compilation".

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

However, since compiled languages ​​mostly operate at the bottom layer and deal with bytes, integers, floating-point numbers or other machine-level objects, it often requires a lot of complex code to implement a simple function. For example, in C++, it is difficult to do something as simple as "copy all the files in one directory to another".

interpreted language

Interpreted languages ​​are also known as "scripting languages". When executing such programs, the interpreter needs to read the source code we wrote and convert it into object code, which is then run by the computer. Because each time the program is executed, there are more compilation processes, so 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 like files and directories; the disadvantage is that they are generally not as efficient as compiled languages. However, the trade-off is that scripting is usually worth it: a simple script that takes an hour to write, the same functionality written in C or C++, can take two days, and generally the script executes fast enough , fast enough to ignore its performance issues. Examples of scripting languages ​​are awk, Perl, Python, Ruby, and Shell.

When to use Shell

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

• Simplicity: Shell is a high-level language; through it, you can express complex operations concisely.
• 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, considering the command limitation and efficiency of Shell scripts, Shell is generally not used in the following situations:

1. Resource-intensive tasks, especially when efficiency is a concern (eg, sorting, hashing, etc.).
2. Mathematical operations that need to handle large tasks, especially floating-point operations, precise operations, or complex arithmetic operations (this situation is generally handled by C++ or FORTRAN).
3. There is a need for cross-platform (operating system) porting (usually using C or Java).
4. Complex applications, when structured programming must be used (requires type checking of variables, function prototypes, etc.).
5. For mission-critical applications that affect the overall system.
6. Tasks with high security requirements, such as you need a robust system to prevent intrusion, cracking, malicious damage, etc.
7. A project consists of a series of dependent parts.
8. Requires large-scale file operations.
9. Multidimensional array support is required.
10. Requires the support of data structures, such as linked lists or data structures such as numbers.
11. It is necessary to generate or operate a graphical interface GUI.
12. Requires direct operating system hardware.
13. Requires I/O or socket interface.
14. Interfaces that require the use of libraries or legacy code.
15. Private, closed-source applications (shell scripts put code in text files that the world can see).

If your application fits any of the above, then consider a more powerful language -- perhaps Perl, Tcl, Python, Ruby -- or a higher-level compiled language like C/C++, or Java. Even so, you will find that using the shell to prototype your application can be very useful during the development step.

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 execution of the script. It is good to know the name. If you use php to write shell scripts, the extension should be php.

Enter some code:

Copy the code The code is as follows:

#!/bin/bash
echo "Hello World !"

 

"#!" is a convention mark that tells the system what interpreter this script needs to execute, i.e. what kind of 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 corresponding directory:

Copy the code The code is as follows:

chmod +x ./test.sh #Make the script have execute permission./test.sh
#Execute the script

 

Note that it must be written as ./test.sh, not 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 there is a test.sh, and only /bin, /sbin, /usr/bin, /usr/sbin, etc. are in the PATH , your current directory is usually not in the PATH, so if you write test.sh, the command will not be found. Use ./test.sh to tell the system to look for it in the current directory.

To run a 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 of the shell (imagine Windows Explorer), but I deliberately write it as the system to facilitate understanding. Since the system refers to the shell, is a script that uses /bin/sh as the interpreter? Can the first line be omitted? Yes.

as interpreter argument

This mode of operation is to run the interpreter directly, and its parameter is the file name of the shell script, such as:

Copy the code The code is as follows:

/bin/sh test.sh
/bin/php test.php

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326713503&siteId=291194637