How to write a simple shell script.

Source: Internet
Author: User

The shell itself is a program written in C, which is a bridge for users to use Linux. The shell is both a command language and a programming language. As a command language, it interprets and executes user-entered commands interactively, and as a programming language, it defines variables and parameters and provides many control structures in high-level languages, including loops and branches.

Although not part of the kernel of the Linux system, it invokes most of the system's core functions to execute programs, create files, and coordinate the operation of individual programs in parallel.

Therefore, for the user, the shell is the most important utility program, in-depth understanding and proficiency in the shell features extremely use of the method is the key to good Linux system.

2. Knowledge Analysis

The shell is a scripting language and there must be an interpreter to execute the scripts

We always say how many shells there are, in fact, the shell script interpreter.

Bash is the default shell used by Linux systems. The bash was completed by Brian Fox and Chet Ramey, a bourneagain shell abbreviation with 40 internal commands.

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

You can use the arrow keys to consult and quickly enter and modify commands.

A command that begins with a string is automatically given by finding a match.

Contains its own Help function, you can just type help below the prompt to get related assistance.

3. Frequently Asked Questions

How to write a shell (bash) script

Why write shell scripts

4. Solution

The shell has two ways of executing commands:

Interactive (Interactive): Explains the execution of a user's command, the user enters a command, and the shell interprets the execution of a single rule.

Batch: The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time.

Shell scripts also have variables and process control statements, but shell scripts are interpreted to execute, do not need to be compiled, and the shell program reads and executes these commands from a line of script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution.

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

Portability: Using POSIX-defined features, scripts can be executed on different systems without modification.

Easy to develop: a powerful and usable script can be completed in a short period of time.

5. Code combat 6. Expand Thinking

What other scripting interpreters are there?

Which cases are not suitable for shell scripting

The SH was developed by Steve Bourne and is the abbreviation for the Bourne shell, and various UNIX systems are equipped with SH.

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

CSH is a Linux-based kernel that consists of 47 authors, represented by William Joy, and has 52 internal commands. The shell is actually a shell that points to/bin/tcsh, which means that csh is actually tcsh.

Ksh is an abbreviation for the Korn shell, written by Eric Gisin, with a total of 42 internal commands. The biggest advantage of the shell is that it is almost completely compatible with the ksh of the commercial distribution, so you can try out the commercial version of the performance without having to pay for the commercial version.

However, given the command restrictions and efficiency of shell scripts, the following situations do not typically use the shell:

1. Resource-intensive tasks, especially when you need to consider efficiency (e.g., sorting, hashing, etc.).

2. Mathematical operations that need to handle large tasks, especially floating-point operations, precision operations, or complex arithmetic operations (which are typically handled using C + + or FORTRAN).

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

4. Complex applications where structured programming is necessary (requires variable type checking, function prototypes, etc.).

5. For mission-critical applications that affect the overall system.

6. Tasks that require a high level of security, such as requiring a robust system to prevent intrusion, cracking, malicious destruction, and so on.

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

8. Large-scale file operation is required.

9. Support for multidimensional arrays is required.

10. Support for data structures, such as linked lists or numbers, is required.

11. Need to produce or manipulate graphical interface GUI.

12. Direct operating system hardware is required.

13. I/O or socket interface required.

14. Need to use the interface of the library or legacy code.

15. Private, closed-source applications (the shell script puts the code in a text file, as the world can see it).

7. References

① Baidu

②http://www.jb51.net/article/52374.htm

How to write a simple shell script.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.