shell

xiaoxiao2021-03-06  109

* SHELL is? Any inventions have an interface for users. The interface for users is used by the user is the shell (DOS's Command, but UNIX is much more powerful). The shell provides users with input commands and parameters and get the environments where command execution results. For different needs, UNIX provides different shells. Most of UNIX support Bourneshell. The following tutorials take Bourneshell (BSH) as an example, step by step to experience the powerful features of UNIX shell, to achieve more convenient and flexible management, apply Unix purposes. 1. The UNIX kernel and the SHELL interaction method starts UNIX, the program UNIX (kernel) will be transferred to the computer memory and keep it in memory until the machine is turned off. During the boot process, the program init will enter the background run until the machine is turned off. The program query file / etc / inittab, which lists the individual ports and features of the connection terminal. When an active terminal is found, the init program calls the Getty program to display login information on the terminal. (UserName and Passwd) After entering the password, getty calls the Login process, which verifies the user's identity according to the content of the file / etc / passwd. If the user passes authentication, the Login process sets the user's Home directory to the current directory and handles the control to a series of setup programs. The Setup program can be a specified application, usually the setup program is a shell program, such as: / bin / sh, Bourne Shell (Command is out, huh). After getting control, the shell program reads and executes files /etc/.profile and .profile. These two files have established a system-wide and the user's own work environment. Final shell displays the command prompt, such as $. (This is an example of BSH, if CSH, for .cshrc, ksh is .Kshrc, Bash is .bashrc, etc.) Note: (Should use /etc/.profile and .profile as DOS's autoexec.bat or config. SYS file) When shell exits, the kernel control is handed over to the init program, and the program restarts the automatic login process. There are two ways to make the shell exit. First, the user performs the exit command, and the other is the kernel (for example, the root uses the kill command) to issue a kill command to end the shell process. After the shell exits, the kernel reclaims the resources used by the user and the program. After the user logs in, the user command is the relationship between the computer interaction: command process ---> shell program ---> UNIX kernel ---> Computer hardware. When the user enters a command, such as $ LS, the shell will locate its executable file / bin / ls and pass it to the kernel. The kernel generates a new sub-process call and execute / bin / ls. When the program is executed, the kernel cancels the child process and handles the control to its parent process, the shell program. For example, the execution: $ PS This command will list the processes that the user is performing, that is, the shell program (discussed in detail, an urgent needs now) and the PS program. If you do: $ SLEEP 10 & $ PS, the first command will generate a Sleep child process executed in the background. The sub process is displayed when the PS command is executed. Whenever the user performs a command, a child process is generated. The execution of the sub-process is completely independent of its parent process or shell, which makes the shell to do other work. (Shell just tells the kernel's intent, then why do you do :)) Now Windows has a planned task (in a fixed time, the date is automatically executed with a task), in fact, Unix has this feature very early, that is The so-called SHELL is automated.

Some UNIX resources, such as CRON can automatically execute the shell program without the need for users, (this function is in / var / spool / crotab directory). The CRONTAB program is very useful for system administrators. The cron service is used to operate at a specific time (month, day, week, time, minute). We take root 's crontab as an example. The root user's crontab file is placed in / var / spool / crontab / root, its format is as follows: (1) (2) (3) (4) (5) (6) 0 0 * * 3 / usr / bin / updatedb 1. Minute (0-60) 2. Hours (0-23) 3. Day (1-31) 4. Month (1-12) 5. Week (1-7) 6. The program to run 2.shell Function and Features 1> Command Line Explanation 2> Using the Reserved Word 3> Using the Shell Element Character (Wi - Caracoring) 4> Processor Command 5> Using Input Output Redirection and Pipe 6> Maintain Some Variables 7> Operation Environment Control 8> Support Shell Programming is not more to say "command line interpretation", just enter a line unix command after the shell prompt (for example, "$", "%", "#", etc., and the shell will receive the user's input. "Use the reserved word": shell has some words with special meaning, such as in the shell script, DO, DONE, FOR et al. To control cycle operation, if, then and other control condition operations. Reserving the word difference with the shell environment. "Tongcar": * Matched any location? Match a single character [] matching character range or list, for example: $ ls [ac] * will list all files at the beginning of the AC range $ ls [A, M, T] * All file "program commands" starting with E, M or T: After the user enters the command, the shell read the environment variable $ PATH (which is typically set in the user's own .profile), the variable contains the command executable A list of directory where files may exist. The shell looks for the executable corresponding to the command from these directories, and then gives the file to the kernel. "Input Output Redirection and Pipes": Redirected functionality with DOS Redirect function: ">" Redirect Output "<" Redirect input and pipe symbols, is a powerful place for UNIX function, symbol is a vertical line: "|", Usage: Command 1 | Command 2 His function is to pass the result of the first command COMMAND 1 to Command 2, for example: $ ls -s | sort -nr | pg All files in the current directory and give the output to the sort command as input, and the sort command sorts the output of the LS by digital decreasing order. Then transmit the sorted content to the PG command, the PG command displays the sort commands on the display. "Maintenance Variable": The shell can maintain some variables. Some data is stored in the variable.

Users can assign values ​​to variables with "=", such as: $ look = / usr / mydir This command establishes a variable named LOOKUP and assigns value / usr / mydir, and the user can use Lookup in the command line to replace / usr / mydir, for example: $ Echo $ LOOKUP results show: / usr / mydir In order to make the variable can be used by the sub-process, you can use the exprot command, for example: $ =UP = / usr / mydir $ export lookup "Run Environment Control": When the user logs in to start After the shell, shell wants to create a job for the user, as follows: 1> When the login program activates the user shell, the environment variable will be established for the user. Read from the / etc / profile and .profile file, in these files, the terminal type is typically used in these files, and set the shell to look for the path to the executable with the $ terminal variable. 2> When the shell is started from the / etc / passwd file or the command line, the user can specify some parameters to the shell program, such as "-x", which can display the command and its parameters before command execution. These parameters are described later. "Shell Programming": The content mainly introduced in this article. The shell itself is also a language (* can be understood as a combination of UNIX commands, plus the category of class C, cycle control statement, similar to DOS batch, but strong), users can program (script, Text file), complete a specific job. Shell Variables Let's introduce the Bourne Shell program: Bourne Shell has been designed since the Bell Lab. Since then, many vendors have designed many versions of UNIX based on different hardware platforms. But in many versions of UNIX, the Bourne Shell has been consistent. 1> BSH start: After the user is logged in, the system launches the Shell according to the information item of the user according to the file / etc / passwd. For example, the information item in PassWD is: ICE_walk:!: 411: 103: IMSNOW, ICE_WALK: / HOME / ICE_WALK: / BIN / BSH indicate that the username is ICE_walk and other information, in the last item "/ bin / BSH "indicates that the user's SH environment is BSH, so the system is started. You can use the following parameters during starting or execution (including the shell program - scripting) that we want to talk about, we can explain: -a Read all variables-C "string" reads commands from String - E use Non-interactive mode -F prohibits the SHELL file name Generate -h definition -i interactive mode -k to execute setting options - N read commands but do not perform -R Limited mode -s command from standard input -T -T Execute a command, then exit the shell -u in replacement, use unset variables will be erroneous-V. Mixed's input line -X tracking mode, and the execution command Many modes can be combined, you can try it, But -ei is not like, do you say why? Use SET to set or cancel the shell option to change the shell environment. Open the option "-", close the option " ", most UNIX allows you to turn the A, F, E, H, K, N, U, V, E, H, K, N, U, V, and X options are allowed to open or close. If you display the option already set in the shell, do: $ Echo $ - BSH has a .profile file in the Home directory of each user, you can modify the file to modify the shell environment.

In order to add a path of executable file (such as / ice_walk / bin), you can add the following code to Path = $ PATH: / ICE_Walk / Bin; Exprot Path .profile meaning the environment variable in the SHELL is as follows: CDPATH executes a CD command Search Path HOME User's Home Directory IFS IFS The domain segmentation of the IFS, generally a space character, a tab, or a newline mail specifying a specific file (mailbox) path, there is a Search path for using the PATH search command (With the DOS config.sys PATH) PS1 master command prompt, the default is "$" PS2 from the command prompt, the default is ">" TERM Using Terminal Type 2> BSH Special Character and its meaning in BSH Non-alphabetic characters. The use of these characters is divided into four categories: as a special variable name, generate file name, data or program control, and reference and escape character control. They allow users to complete complex tasks using the least code of the shell. *> SHELL Variable Name Used Special Character $ # Transfer to the Parameter Serial No. $ of the Command SHELL $ - Provides an option $ when the shell starts or uses the set command? The value returned after the last command is executed $$ Current shell process number $! A child process number $ @ All parameters, each parameter is included in brackets, enclose the $ n position parameter value with double brackets, n Represents $ 0 Current shell name *> Special Characters include "*", "?", "[]", Taking above, no longer said. *> Data or Program Control Used Special Character> (File) Output Redirection to the file (Created, Overwrite without File) >> (File) Output Redirection to the file (not created, there is an additional to File Tail) <(file) input redirection to file; Command Solution | Pipes & Background Run (for example: Sleep 10 &) `` command Replace, redirect the output of a command as another command *> For reference Or escape special character BSH uses single quotes '' and dual quotes "" "" "" "" "" "" The parameters and variables are replaced. Escape characters are also the same. $ Echo "$ home $ path" results show $ / u / ice_walk / bin: / etc: / usr / bin and $ Echo '$ home $ path' results Show $ HOME $ PATH SHELL Escape is a "", indicating that the following characters do not have special meaning or not the shell function $ Echo $ home $ PATH results show $$ Home / bin: / etc: / usr / bin: 3> BSH We will reference the variables in front of us, when the shell encounters a "$" character (not being referenced or escape), it will think thereafter it is a variable. Whether the variable is an environment variable or a user-defined variable, the variable name in the command line is to be replaced by the variable value. For example, the command: LS $ Home will list the files in the directory of the variable home. Users can replace variables anywhere in the command line. Includes command name itself, for example: $ dir = ls $$ dir f * will list files starting with F. Now introduce the variable of BSH.

There are four types of variables in BSH: user-defined variables, position variables (shell parameters), predefined variables and environment variables. User-defined variable: user-defined variable consists of letters and underscores, and the first character of the variable name cannot be numbers (0 ~ 9). Like other UNIX names, the variable name is sensitive. The user can assign a value to the variable on the command line, for example: $ name = ICE_walk assignment to Ice_walk, when applying the variable Name, before NAME, "$", the previous, no longer Nance (don't say that I have more nonsense, the key is that there is no teacher: (). You can make new words with variables and other characters, such as: $ sun = sun $ echo $ {sun} day at the application shell variable, you can The variable name is added to the {}, with a clearer display to the shell, which is the real variable to implement the merger of the string. The result shows: Sunday (Note You cannot Echo $ Sunday, because the sunday variable is not defined, The execution result of the reader's test) The user can also assign a number of variables on the command line. The assignment statement is separated from spaces: $ x = x y = y Note that the variable assignment is $ x = $ yy The value of y x is Y $ x = z y = $ z y is empty (when the variable is not assigned, the shell is not reported, but the value is empty) The user can use the "Unset " command to clear the variable The value of the user uses the variable to add a "$" in front of it, so that the variable name is replaced by the variable value. BSH can be replaced with the condition of variables, that is, only some of the conditions occur, replace it. Replacement conditions For large parentheses {}, such as: $ {variable: -value} variable is a variable, Value is the default value for variable replacement $ Echo Hello $ uname results display: Hello $ Echo Hello $ {uname: -there} Display: Hello The $ echo $ uname results display: (empty) $ uname = john $ echo hello $ {uname: -there} results show: Hello John can see that the default value defined in the command line is used when variable replacement is used. However, the value of the variable is not changed. Another alternative method is not only replaced by the default value, but also assigns the default value to the variable. The form is as follows: $ {variable: = value} This form is replaced by variable At the same time, the value value is paid to the variable Variable.

$ echo hello $ uname results display: Hello $ Echo Hello $ {uname: = there} results show: Hello the $ echo $ uname results display: there is $ uname = john $ echo hello $ {uname: -there} results show: Hello The value of the John variable can also be `` enclosed command: $ userDir = {$ mydir: -`} The alternative method of the third variable is that only the specified value is replaced by the specified value when the variable has assigned: $ { VARIABLE: VALUE} Only when Variable is assigned, its value is replaced with Value, otherwise no replacement, for example: $ erropt = a $ Echo $ {Erropt: "Error Tracking is Acitive"}}} t TRACKING IS Acitive $ Erropt = $ {Erropt: "Error Tracking is Acitive"} The result is displayed: (empty) We can also use the error checking criter replacement: $ {variable:? message} When the variable variable is set Normal replacement. Otherwise, Message will be sent to standard error output (if this replacement appears in the shell program, then the program will terminate). For example: $ uname = $ echo $ {uname:? "Uname Has NOT BEEN SET"}: Uname Has Not Been set $ uname = Stephanie $ Echo $ {uname: • "Uname Has NOT BEEN SET"} results show: Stephanie When you do not specify Message, the shell will display a default message, such as $ uname = $ echo $ {uname :?} results display: sh: uname: parameter null or not set4> Location variable or shell parameter at the shell interpretation user When the command is command, the first word of the command line will be used as a command, while the other words are as a parameter. These parameters are transmitted to the program as position variables when the executable of the command is a shell program. The first parameter is $ 1, the second is $ 2 .... ninth is $ 9. Where 1 to 9 are real parameter names, "$" is just a replacement of the variable. Location Variable $ 0 means the executable file name corresponding to the command. The location variable will be described in detail later. 1. Reemption Variable User After assigning the variable, in order to prevent the modification of the variable in order to prevent this variable to read-only variables: Readonly variable 2.export command shell When executing a program, first establish a program A new execution environment called a child shell. The variables in the Bourne Shell are part, that is, they are only meaningful in creating their shell. Users can use the export command to let the variables are identified by other child shells. However, the variable of a user cannot be used by other users. When the user starts a new shell, the shell will use the default prompt. Because the value assigned to the variable PS1 is only valid in the current shell.

转载请注明原文地址:https://www.9cbs.com/read-103704.html

New Post(0)