What is shell?

Source: Internet
Author: User

Introduction
You can do many things without knowing how they actually work. For example, you don't have to understand the physical principle of engine combustion before you can start a car. Lack of electronic knowledge will not stop
You use a CD server to listen to music. I don't know what shell is and how it works. You can still use Unix. However, you can make better use of UNIX.
A Unix system has three representative shells available: Bourne
Shell, KoRn shell, and C shells. They will be discussed in chapter 11, 12, and 13. In this chapter, you will learn:

What is shell?
What can shell do for you?
Relationship between shell and the entire system

Kernel and Shell

The nut shell protects its internal core, the same Unix
Shell provides a protection layer for peripherals. When you start a UNIX-based computer, Unix programs are transferred to the computer's memory until you shut down. This program is called internal
Kernel, which performs a lot of bottom-level and system-level work. The kernel has the responsibility to explain basic commands and send them to the processor. The kernel is also responsible for running and scheduling processes, as well
Some input and output. The kernel is the heart of a UNIX system, and there is only one kernel.

You may be confused about the key responsibilities of the kernel, and kernel commands are equally complex and highly technical. To shield users from kernel complexity, and to protect the kernel
As a result, a shell is built around the kernel ). The user sends a request to the shell. The shell interprets the request and sends the request to the kernel. The rest of this section explains the outer layer.
Is how to establish.

Once the kernel is transferred to the memory, it is ready to execute the user's request. However, the user must first connect to log on and then send a request. In any case, the kernel must know that the user is
Who and how to talk to him. To do this, the kernel calls two special programs, Getty and login. correspond to each user's logon point-usually known as a tty-kernel call.
Getty program. This process is called spawning ). Getty displays a logon prompt, and then constantly monitors the call point and waits for the user name input. When
When Getty gets any input, it calls the login program. Login establishes a user's identity and verifies his or her login right. The login program checks the password file. If you use
If the password entered by the user is incorrect, the control will return to Getty from the logon point. If it is correct, login calls the program recorded in the user entry in the password file and gives control to it. This
A program may be a word processing software or spreadsheet program, but it is generally called a shell program.

Assume that four users have logged on to the system. Two of the four users are using the Bourne shell and one is using the KoRn
Shell, and a spreadsheet program. Every user gets a copy of the shell to serve its request, but the kernel has only one. Using shell will not impede user use
Workbooks or other programs, but those programs run under the active shell. Shell is a single user-specific program, which provides a field between the user and the UNIX Kernel
.

You do not have to use shell to access UNIX. In the preceding example, a user replaced shell with a workbook. When the user logs in, the workbook program starts.
When it exits the spreadsheet program, it exits the system. This technology is useful when you emphasize security or want to block users from any UNIX interface. Disadvantage:
Users cannot use mail or other UNIX functions.

Because login can execute any program-shell is just a simple program-you may write your own shell. In fact, three independently developed shells have become Unix
Standard part. They are:

Bourne shell, developed by Stephen Bourne
Korn shell, developed by David KoRn
C shell, developed by Bill Joy

The diversity of shell allows you to choose the interface that best suits you or feels closest to you.

Shell functions

However, the standard shell you choose does not matter much, because the three shells share the same purpose: to provide users with an interface in UNIX. To achieve this goal, three
Shell provides the same basic functions:

Command Line explanation
Start the program
Input/Output redirection
MPs queue connection
File Name replacement
Variable Maintenance
Environment Control
Shell programming

Command Line explanation

When you log in and start an interactive shell, you will see a shell prompt, usually in the form of $, % or # symbol. After you enter a line of characters at the prompt,
Shell tries to explain it. Input at a shell prompt is sometimes called a command line. The basic format of the command line is
Command arguments
Command name parameters (one or more ))

Command is an executable Unix Command, program, utility, or shell program. Arguments (parameter) is passed to the execution program. Most Unix instances
The format of parameters required by the program is as follows:
Option filenames
Option file name (one or more ))

For example

$ LS-l
File1 file2

In this command line, there are three parameters passed to LS, the first is an option, and the remaining two are file names. One of the things shell does for the kernel is to reduce unnecessary information. For
Whitelist (Whit-Espace) is a type of useless information. Therefore, it is necessary to know what shell does when it encounters a blank space. Blank spaces are composed of spaces, horizontal tabs, and line breaks.
Consider this example:

$ Echo Part A Part B Part C

Part A Part B Part C

Here, the command line is interpreted as an echo command with six parameters and the blank space between parameters is deleted. If you are printing a report header and want to keep it blank, enclose the data in quotation marks.
As shown below:

$ Echo Part A Part B Part C

Part A Part B Part C

Single quotes prevent shell from checking the quotation marks. Now shell interprets this line as an echo command with a parameter, and this parameter exactly contains a blank string.

Start the program

After shell interprets the command line, it starts the program required by the command line. The kernel actually runs this program. To Start Program Execution, shell specifies
To search for executable files. When it finds the file, it starts a sub-shell to run the program. You should know that the sub-shell does not have to affect its father's environment settings and is built and manipulated from
Environment. For example, a shell can change its working directory. After running the shell, the working directory of its parent shell remains unchanged.

Input/Output redirection

Shell redirects before executing the program. Consider the following two examples. The WC word statistics tool is used to collect data files with five rows:

$ WC-l fivelines

5 fivelines

$ WC-L 5

There is a slight difference here. In the first example, WC knows that it should go out to find and operate a file named fivelines. WC knows the file name, so it
Show to the user. In the second example, WC only sees the data and does not know where the data comes from, because shell does the job of locating and redirecting the data to WC, so WC cannot
Display file name.

MPs queue connection

The pipeline is a special case of input/output redirection. It connects the output of one command directly to the input of another command. Therefore, the pipeline was created before the program was called. Consider the following
Line:

$ Who | WC-l

5

Shell does not display the output of WHO to the screen, but directs the input to WC.

File Name replacement

Shell has the responsibility to replace the file name. Shell is replaced before executing the program. For example:

$ Echo
*

File1 file2 file3 file3x file4

Here, the asterisk is extended to five file names and passed to echo as five parameters. To display an asterisk, enclose it in quotation marks.

Variable Maintenance

Shell is capable of variable maintenance. Variables are the places where data is stored for future use. You can assign values to variables with equal signs (=.

$ Lookup =/usr/mydir

Here, shell creates a lookup variable and assigns/usr/mydir to it. Later, you can use the variable value on the command line by adding the $ symbol before the variable name. Considerations
Examples:

$ Echo $ Lookup

/Usr/mydir

$ Echo Lookup

Lookup

C-shell users should note that the value assignment in C-shell is different from that in Bourne and Korn.
Shell. C-shell uses the SET command to assign values.

$ Set lookup =
/Usr/mydir

Note that spaces must be added on both sides of the equal sign.

Just like the replacement of file names, the replacement of variable names is performed before the program is called. In the second example, the $ symbol is omitted. Therefore, shell simply transmits the string to echo
Parameters. In variable replacement, the variable value replaces the variable name. For example:

$ Ls $ lookup/filename

Use/usr/mydir/filename as the parameter to call ls.

Environment Control

When the login program calls your shell, the shell sets your environment, including your home directory, the terminal type you use, and
Path. The environment is stored in environment variables. For example, to change the terminal type, you need to change the value in the term variable, as shown below:

$ Echo $ term

VT 100

$ Term = ANSI

$ Echo $ term

ANSI

Note that the setenv command is used to assign values to environment variables in C-shell.

% Setenv term
VT100

Shell programming

You have seen that shell can be used to explain command lines, maintain variables, and execute programs. In addition, shell is a programming language. Use Process Control and condition judgment to combine commands and changes
You have a powerful programming tool. Using shell as a programming language, you can automate repetitive tasks, write reports, and even create and manipulate custom
Your data files.

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.