What is a shell?

Source: Internet
Author: User
Tags echo command posix

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.

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

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 are similar to programming languages, and there are variables and process control statements, but shell scripts are interpreted and do not need to be compiled, and the shell program reads and executes the commands from a single line of script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution.

For Shell Beginners Please note that in a normal application, it is recommended that you do not run the shell with the root account. As a normal user, no matter whether you intentionally or unintentionally, can not destroy the system, but if it is root, it is different, as long as a few letters, can lead to disastrous consequences.

A few common shell

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

Common shell scripting interpreters on Linux include bash, sh, ash, csh, and Ksh, which are used to call them a shell. We often say that there are many kinds of shells, actually speaking of shell script interpreter.

Bash

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 functions of doskey like DOS to view and quickly enter and modify commands with the arrow keys.
? automatically gives a command that starts with a string by looking up a match.
You can get help by typing help below the prompt, including your own helper features.

Sh

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

Ash

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

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

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.

The difference between a shell and a compiled language

In general, the programming language can be divided into two categories: the compiled language and the interpreted language.

Compiled languages

Many of the traditional programming languages, such as Fortran, Ada, Pascal, C, C + +, and Java, are compiled languages. These languages require that the source code that we have written be converted into a target tag (object codes), and this process is 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, the execution is very efficient, which is the advantage of a compiled language.

However, since most of the compiled languages work on the ground floor, dealing with bytes, integers, floating-point numbers, or other machine-level objects, it is often necessary to implement a simple function that requires a lot of complex code. For example, in C + +, it's hard to do simple things like "copy all files from one directory to another."

Interpreted language

Interpreted language is also known as the "scripting language". When executing such a program, The interpreter (interpreter) needs to read the source code that we wrote and convert it to the target (object), which is then run by the computer. Because each execution of the program is more than the compilation process, so efficiency has decreased.

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, and the disadvantage is that they are often less efficient than compiled languages. However, the tradeoff is that scripting is usually worthwhile: a simple script that takes one hours to write, the same functionality written in C or C + +, can take up to two days, and, in general, the speed of the script execution is fast enough to let people 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 is standardized by POSIX. As a result, shell scripts can be applied to many systems once they are "written attentively". Therefore, the reason to use shell scripts is based on:

Simple: The shell is a high-level language, and through it, 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 妤 script can be completed in a short time.


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).

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

First shell script

Open a text editor, create a new file, the extension sh (sh for Shell), the extension does not affect the execution of the script, see the name is good, if you write a shell script in PHP, the extension is good with PHP.

Enter some code:

Copy CodeThe code is as follows:
#!/bin/bash
echo "Hello world!"

"#!" is a contract tag that tells the system what interpreter the script needs to execute, even if it uses a shell. The echo command is used to output text to a 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 CodeThe code is as follows:
chmod +x./test.sh #使脚本具有执行权限
./test.sh #执行脚本

Note, be sure to write./test.sh, not test.sh. Run other binary programs also, the direct write Test.sh,linux system will go to the path to find there is not called test.sh, and only/bin,/sbin,/usr/bin,/usr/sbin, etc. in path, your current directory is usually not in path, So write test.sh will not find the command, to use./test.sh tells the system that it is looking in the current directory.

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

The "system" here is actually the shell application (imagine Windows Explorer), but I deliberately write the system, is easy to understand, since this system refers to the shell, then a script using/bin/sh as an interpreter can save the first line? Yes.

As an interpreter parameter

This works by running the interpreter directly, whose parameters are the file names of the shell scripts, such as:

Copy CodeThe code is 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.

What is a shell?

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.