What is a shell? Shell Scripting Basics Detail _linux Shell

Source: Internet
Author: User
Tags echo command posix

The shell itself is a program written in C language, which is a bridge for users to use Linux. Shell is both an imperative language and a programming language. As the command language, it interprets and executes the commands that the user enters interactively; 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 core of the Linux system, it invokes most of the functions of the system's core to execute programs, build files, and coordinate the operation of each program in a parallel manner. Therefore, for the user, the shell is the most important utility, in-depth understanding and proficiency in the shell of the characteristics of the extremely use of a good Linux system is the key.

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

The shell has two ways to execute a command:

Interactive (Interactive): interprets the execution user's commands, the user enters a command, and the shell interprets the execution.
Batch processing (Batch): The user writes a shell script in advance, with many commands that allow the shell to execute the commands one at a time without having to hit the command one by one.

Shell scripts and programming languages are similar, there are variables and process control statements, but shell scripts are interpreted to execute, do not need to compile, the shell program from the script to read and execute the command line, the equivalent of a user to the script in the command line to the shell prompt execution.

Shell beginners Note that in normal applications, it is recommended that you do not run the shell with the root account. As an ordinary user, whether you intentionally or unintentionally, can not destroy the system, but if it is root, it is different, just knock a few letters, can lead to disastrous consequences.

A few common shell

As mentioned above, the shell is a scripting language, so you have to have an interpreter to execute the scripts.

The common shell script interpreters on Linux are bash, sh, ash, csh, and Ksh, which are customarily referred to as a shell. We often say that there are many kinds of shells, in fact, the shell script interpreter.

Bash

Bash is the shell that the Linux system uses by default. Bash is done by Brian Fox and Chet Ramey, the acronym for the Bourneagain Shell, with 40 internal commands.

Linux uses it as the default shell because it has features such as the following:
• You can use the Doskey function similar to DOS, and use the arrow keys to access and quickly enter and modify commands.
• Automatically gives commands that begin with a string by finding a match.
• Contains its own Help features, and you can get help by typing it below the prompt.

Sh

SH developed by Steve Bourne, is the abbreviation of Bourne shell, all kinds of UNIX systems are equipped with SH.

Ash

The ash shell, written by Kenneth Almquist, is a small shell that consumes the least amount of system resources in Linux, which contains only 24 internal commands and is therefore inconvenient to use.

Csh

CSH is a larger kernel of Linux, composed of a total of 47 authors, represented by William Joy, with a total of 52 internal commands. The shell is actually pointing to a shell such as/bin/tcsh, that is to say, CSH is actually tcsh.

Ksh

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

The difference between a shell and a compiled language

In general, you can divide a programming language into two categories: a compiled language and an interpreted language.

Compile-language

Many traditional programming languages, such as Fortran, Ada, Pascal, C, C + +, and Java, are all compiled languages. This kind of language needs to advance our written source code (source code) to the target (object codes), a process called "compiling".

When you run the program, read the object code directly. Because the compiled object code is very close to the bottom of the computer, execution is highly efficient, which is the advantage of a compiled language.

However, since a compiled language is mostly run at the bottom, dealing with bytes, integers, floating-point numbers, or other machine-level objects, it often takes a lot of complex code to implement a simple function. For example, in C + +, it is very difficult to "copy all the files in one directory to another directory," such as simple operations.

Explanatory language

Interpreted languages are also called "Scripting languages." When executing such a program, The interpreter (interpreter) needs to read the source code that we have written and convert it to the target (object codes) and run by the computer. Because each execution program has a more compiled process, the efficiency is reduced.

The advantage of using scripting languages is that they are mostly run at a higher level than the compiled language and can easily handle objects such as files and directories; The disadvantage is that they are often less efficient than compiled languages. But on balance, scripting is usually worthwhile: a simple script that takes one hours to write, the same functionality is written in C or C + +, it can take two days, and generally, the script executes fast enough to allow people to ignore its performance problems. Examples of scripting languages are awk, Perl, Python, Ruby, and Shell.

When to use the shell

Because the shell appears to be a common feature between UNIX systems, and has been standardized by POSIX. As a result, the shell script can be applied to many systems as long as it is written "attentively". Therefore, the reason to use shell scripts is based on:

• Simplicity: The shell is a high-level language through which you can express complex operations succinctly.
• Portability: With the functionality defined by POSIX, scripts can be executed on different systems without modification.
• Easy to develop: a powerful and Hsueh script can be completed in a short period of time.


However, given the command restrictions and efficiency issues with the shell script, the following situations do not typically use the shell:

1. Resource-intensive tasks, especially when it is necessary to consider efficiency (e.g., sequencing, hashing, etc.).
2. Mathematical operations that require handling large tasks, especially floating-point operations, precise operations, or complex arithmetic operations (which are typically handled using C + + or FORTRAN).
3. There is a cross-platform (operating system) porting requirements (generally using C or Java).
4. Complex applications, when it is necessary to use structured programming (type checking of variables, function prototypes, etc.).
5. Critical task applications that affect the overall system.
6. Tasks that require high security, such as the need for a robust system to prevent intrusion, cracking, malicious destruction, and so on.
7. The project consists of various parts of a chain of dependencies.
8. Large-scale file operations are required.
9. Support for multidimensional arrays is required.
10. The need for data structure support, such as linked lists or numbers, etc.
11. Need to produce or manipulate GUI of graphical interface.
12. Direct operating system hardware is required.
13. Need I/O or socket interface.
14. Interfaces that require the use of libraries or legacy code.
15. Private, closed source applications (the shell script puts the code in a text file that the world can see).

If your application matches any of the above, consider a more powerful language--perhaps Perl, Tcl, Python, ruby--, or a higher-level compiled language such as C + +, or Java. Even so, you'll find that using a shell to prototype your application is also useful in the development step.

First shell script

Open a text editor, create a new file, the extension is sh (sh represents shell), the extension does not affect the script execution, see the name is good, if you use PHP to write the shell script, the extension will use PHP.

Enter some code:

Copy Code code as follows:

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

"#!" is a convention tag that tells the system what interpreter is needed to execute the script, even with which shell. 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:

Copy Code code as follows:

chmod +x./test.sh #使脚本具有执行权限
./test.sh #执行脚本

Note that you must write./test.sh, not test.sh. Run other binary programs as well, directly write Test.sh,linux system will go to the path to find there is no call test.sh, and only/bin,/sbin,/usr/bin,/usr/sbin, etc. in the path, your current directory is usually not in path, So written test.sh will not be able to find the command, to use./test.sh told the system that it was looking in the current directory.

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

The "system" here is actually the shell application (imagine Windows Explorer), but I intentionally wrote the system to make it easy to understand that since the system refers to the shell, does a script that uses/bin/sh as an interpreter omit the first line? Yes.

As an interpreter parameter

This is run by running the interpreter directly, whose argument is the file name of the shell script, such as:

Copy Code code as follows:

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

The script that runs this way does not need to specify the interpreter information in the first line, and it is useless to write it.

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.