Sender: Albin (Aile Bin), News District: Linux
Title: Linux Elective Class: Shell Introduction
Sending station: Bihai Qingtian (Wed Apr 21 23:08:17 1999), transfer
Copyright Notice:
This article is the Linux elective course of Dalian University of Technology. You are welcome to reprint, but it is forbidden to use this material.
Any commercial or profitable event. Please keep this copyright statement when you reprint.
Author: He Binwu, hbwork @ dlut.edu.cn, Network Center of Dalian University of Technology, April 1999.
URL: ftp://ftp.dlut.edu.cn/pub/people/albin/
-------------------------------------------------- ----------------------------
UNIX shell
Overview
Unix common shell
Bourne Shell (SH, Bash), Korn Shell (KSH), CSH (CSH, TCSH)
Related functional / knowledge with each shell:
The relationship between shell and UNIX
Specialty of Shell.
. Set of shell environment variables
2. The interaction of shell and kernel
The system is launched during the system (/ unix, / vmunix, / boot / zimage, etc.) will be loaded to the memory until the system is turned off, starting
During the process, init will scan / etc / inittab, which will list the available terminals and their properties in this file, once found
Terminal, getty (mingetty, linux) will give the login prompt and password. After confirmation, it will start the corresponding
Shell (/ etc / passwd file in the shell domain).
The flow chart is as follows:
UNIX KERNEL -----------> Init ----- -------> getty <-> username and password
^ ^ (Prompt user input user
| | | | Name and password)
V | v
/ etc / inittab | login (read user database / etc / passwd)
(Init configuration file) |
| | |
----- User correct?
| NO
| | | YES
| | |
| V (/ ETC / Passwd file user shell)
------- Shell <----------
EXIT |
| | |
| | |
- User Command ---
In the above figure, getty prompts to enter the user and password, and then pass the username and password to login.
Login verifies whether the user and password match, if the identity verification passes, login will automatically go to its $ home, and
Start the shell program listed in the / etc / passwd domain, then hand over the control to the started task (in shift
The setgid, setuid is completed, respectively, respectively. For example, the user's shell domain in / etc / passwd file is / bin / sh:
SH -> Read file / etc / profile -> Read file $ homen / .profile
System defines variable user-defined variables
------------------------------------------------ - |
V
Give the shell prompt $ PROMPT, default $ ------------------
|
| | |
| | |
-------- Execute User Command -------------
|
Exit
| End
V
Control is handed over to INIT
Shell and sub-process: hierarchical relationship
------------------
| User Process |
| ---------------- |
| | Shell | |
| | ------------ | |
| | | | UNIX KERNEL | | | |
| | | | -------- | | |
| | | | | Hardware | | | | |
| | | | -------- | | |
| | ------------ | |
| ---------------- |
------------------
3. SHELL automatic execution function
Start in the first row in Shell Script: #! / Bin / sh
4. SHELL function and characteristics
Command line explanation
Reserved word
Wildcard
Access, processing command
Document processing: I / O redirection and pipeline
Command replacement
Variable maintenance
Environmental control
. SHELL programming
. SHELL commonly used character meaning
Interpretation of each function and characteristics:
Command line explanation, remove unnecessary information, then pass to the kernel processing
[HBWork @ Helius HBwork] $ Echo Part A Part B Part C
Part a part b part c
[hbwork @ helily hbwork] $ echo "part a part b part c"
Part a part b part c
Reserved word
Do, Done, For, While, Shift, IF, THEN, ELSE, FI
Wildcard
? * []
Access, processing command
$ PATH -> True Path Name To Kernel
Document processing: I / O redirection and pipeline
$ ls> filelist
$ wc -l files (= wc -l #ls -l | sort -nr | more Command replacement`s command` Change the files of all file extensions to filename.tgz #! / bin / sh For i in * .tar.gz DO MV $ I `basename $ i .tar.gz`. TGZ DONE Shell script of the system log file through the crontab task #! / bin / sh FILENAME = `Date" % y% m% D "` Cp / var / log / syslog / backup / log / $ filename Variable maintenance SH: lookup = / home / mydir Export Lookup (lookup variable) CSH: SET LOOKUP = / Home / MyDir SetENV Lookup / Home / MyDir Variable replacement: Path = / usr / local / bin: $ path; EXPOT PATH. Environment Control When the user enters the UNIX environment, the corresponding shell will automatically start, and the shell will establish a specific environment to the user. This environment includes the following common environment variables: TERM, PATH, etc. SH Environment Variable File: / etc / profile $ homen / .profile CSH: /etc/csh.cshrc $ homen / .cshrc Shell Start Options: SH -X script_file_name syntax check SH -I no connection terminal . SHELL programming In addition to a command line interpreter, SHELL is also a powerful programming language, most UNIX The system's startup file (RC files) uses shell script. SHELL commonly used character meaning $ SHELL variable name, such as $ VAR | Pipeline, transfer standard output to the standard input of the next command # 注释 开始 & Execute a process in the background ? Match a character * Match 0 to multiple characters (unlike DOS, you can use in the middle of the file name, and include.) $ # Parameters to Shell Script $ * Parameters passing to Shell Script $? Return code for the last command $$ Process Identifier Number, PID) > Output Redirection
`Command Replace, such as FileName =` basename / usr / local / bin / tcsh` >> Output Redirection, Append [] List the range of characters, such as [A-Z] Filename (BSHELL, BASH) Executive File FileName Source FileName (CSH) Execute File FileName Summary: Shell is an interface of the user's UNIX kernel, performs command interpretation (keyboard input), perform filename and variable Replace, redirect I / O, locate executable, Shell creates a child process and manage it; at the same time shell Maintaining user environment variables is a powerful programming language. - [1; 36M ※ Modified:. Albin modified this article at Apr 21 23:10:27. [From: apple.dlut.edu.c] [M [M [1; 32M ※ Source:. Bihai Youth bbs.dlut.edu.cn. [From: apple.dlut.edu.c] [M