Shell Programming

xiaoxiao2021-03-06  72

Shell Programming http://www.fanqiang.com (2002-01-29 20:11:53)

In DOS, you may engage in some routine reproducibility, and you will write these prime commands into batches, as long as this batch file is equal to executing these commands. Everyone will ask if there is a lot of batch in UNIX, and the answer is there. In Unix, there is not only a batch processing such as DOS, and its function is more powerful than DOS, and it is more complicated relative to the general high-order language. Everyone is not called a batch file in UNIX, and it is called Shell Script. In general, the status of Shell Script and other executable (or commands) is exactly the same, but shell Script is stored in a text-file, not a binary file. When you perform Shell Script, you must have a program to turn its content to a command, and this program is actually shell, which is why we call Shell Script reasons (we call Script). Different shell's SCRIPT will basically have some differences, so we cannot use Script written to the A shell to execute with B SHELL. Everyone in UNIX uses Bourne Shell and C shell, so this class introduces the writing of these two Script. Setting the text file to executable shell script If we have written Script, how to set it into an executable file? Because Script is actually an executable file, it must be set to be executable. We can use the following command to change access rights: chmod u x filename can only be implemented, other people can't perform CHMOD UG X filename can only be implemented, and others cannot perform CHMOD X FileName owners can be implemented. And how do we specify that using that shell to explain the Script written? Several basic specified methods are as follows: 1. If the first non-empty white font element of Script is not "#", it will use the Bourne Shell. 2. If the first non-empty white font of Script is "#", but does not start with "#!", It will use C shell. 3. If Script starts with "#!", "#!" Is written later is the shell used, and the entire path name is to be indicated. Here is recommended to specify the shell to make sure that the executed is to. The path name of the Bourne Shell is / bin / sh, and C shell is / bin / csh. 1. Use bourne shell ┌ ---------- ┐ ┌ ---------- ┐ │ │ #! / Bin / sh │ │ │ │ │ │ │ │ │ │. │ │ │ │ │ │ │ └ ---------- ┘ └ ---------- ┘ 2. Use c shell --------- - ┐ ┌ ---------- ┐ │ # c shell script │ │ #! / Bin / csh │ │. │ │ │ │ │ │ │ │. │ │ │ └ --- ------- ┘ └ ---------- ┘ 3. Use / etc / perl ┌ ---------- ┐ │ #! / Etc / perl │ │. │ │ │ │ │ │ │ │. │ │. │ └ ---------- ┘ In addition to specifying the shell used in Script, you can also force specified in the command column.

For example, if you want to execute a script with c shell, you can ask this command: CSH filename At this time, the access rights of the Script at this time are not necessarily the executable, and the shell specified within it will be invalid, after detail Will discuss. □ Script Basic Structure and Concept Script is in a unit of behavior, and the Script we have written will be decomposed into a row. Each row can be a command, annotation, or process control command, and the like. If a certain row has not been completed, you can add "/" at the end of the line, this time, the content of the next line will receive this line, become the same line, as below ---------- ┐ ┐ ┐ │ c ┌ The Message IS / │ │ │TOO Split IT INTO / │ │SEVERAL LINES │ └ ----------- ┘ When "#" appears in Script, it will follow The same line is annotated, and the shell will not translate it. In Script, you want to perform a command and in the command column, you can perform some environment variables when executing the command, and execute the command. Script's process control and general high-order process control have no two, and it is as follows as high-order language. These make Script more powerful. In order to achieve the same effect as the high-order language, we can also set variables in Script, so that Script becomes a high-order language that is in active. □ Bourne shell 1. Variable Bourne Shell's variable type only a string variable, so use numerical operations must be achieved by external commands. The species of variables have the following: 1. Users variables This is the most common variable, we can use a string that does not contain blank character as a variable name. When setting the variable value, use the following method: VAR = String When you use the variable, you will add a "$" before the variable name. ┌ ------- ┐ │NAME = Tom │ │ │ECHO $ ​​Name │ └ ------- ┘ The result is as follows: Name Tom 2. System variable (environment variable) and user variables However, only this variation passes its value to the commands performed. To set a system variable as a system variable, as long as: export var ┌ ------- ┐ │NAME = Tom │ │EXPORT NAME │ └ ------ ┘┘ ┘ below is the user After entering the system, the system variable has been set: $ HOME user's own directory $ Path Execute the command When the command is executed, the directory of $ TZ Time zone $ mailcheck Every second check whether there is a new letter $ PS1 in the command The prompt number $ PS2 When the command has not yet been finished, the shell requests the prompt number $ MANPATH MAN instruction to re-enter 3. The only-read user variable and the user variation are similar, but these variables cannot be changed. To set the user variable, just add: Readonly Var, if Readon is just readonly, all the only-read variables will be listed. Another point, the system variable cannot be set only.

┌ ------- ┐ │NAME = Tom │ │ │Echo $ Name │ │Name = John │ │Radonly │ └ ------- ┘ The result is as follows: Tom Name: Is Read Only Readonly Name readonly ... 4. Special variables Some variables are set when they begin Script, and do not modify, but we don't call it the system variables read, and call it special variables (some books Will call it only to read the system variables), because these variables are in an executable program, and the user cannot set the general system variables to read. Here are some equivalents: $ 0 execution name $ n This item n = 1..9 $ * This program all parameters $ # This number of parameters $$ This program PID $! Perform the PID $ of the previous background instruction to execute the return value of the previous instruction When you perform this number of parameters more than 9, we can use the shift command to move the parameters forward, so that you can use it. The 10th parameters later. In addition, I can change the $ n and $ * with the set command, the method is as follows: Set String is the value of this $ * is String, and the decomposition will be placed in $ n. If there is no parameters after the set command, all set variables and their values ​​are listed. Delivery: this is a test ┌ ----------- ┐ │Echo Filename: $ 0 │ │ │ │ │ │ of args .: $ # │ │ c 2nd Arg .: $ 2 │ │ n n args .: $ # │ │ │ │ etT hello, Everyone │ │ Echo arguments: $ * │ │ e 2nd arg .: $ 2 │ └- ---------- ┘ The results are as follows: FILENAME: EX1 Arguments: this is a test no. of args .: 4 2nd arg .: is no z .: 3 2nd arg .: A arguments: hello Everyone 2nd arg .: Everyone is worth mentioning that when you want to enter a variable value from the keyboard, you can use the following command: read var1 var2 ..... At this time, READ will give a word to a variable . If the input word is more than the variable, the last variable will regard the remaining words as its value. If the input word is less than the variable, the rear variable will be set to a empty string. If you need to process numeric operations, we can use the expr command. Its parameters and output are listed in Appendix A. Second, the execution command has five methods in the Bourne Shell to execute a command, and the fruits produced in these five modes are different. 1. Direct or order this way and the effect of directly under the command column. 2. Use the SH command SH command This file must be a Bourne Shell's script, but this file does not have to be set up. In addition to this, the way directly under the order. 3. Use "." Command. At this time, and use the SH command, but it is not like SH usually generates new Process, which will do work in the original Processs. 4. Using the exec command EXEC Command At this time, this Script will be replaced by the command executed. This script will end when this command is executed. 5. Use the command to replace this is a quite useful method.

This method must be used if you want to make the output of a command becomes the parameters of another command. We will tell the commands between two "` "numbers, and the shell will replace this command and two" `" symbols in the output result executed by this command. Str = 'current directory is' `pwd` Echo $ STR results are as follows: current directory is / users / cc / mgtsai this means PWD this command output" / users / cc / mgtsai ", and the whole string is replaced by the original` PWD` Set the STR variable, so the content of the STR variable will have the output of the PWD command. Number = `expr $ Number 1` This is the method of previously raised numerical calculations, basically the EXPR command only solves the arithmetic solution, and then outputs it to the standard output. If you want to set a variable to its value, it is not possible to replace the way. This example is the value of the Number variable to the Number variable. Third, the process control before introducing the process control, let's take a look at the test command. The parameters of the TEST command are conditional judgments, and when the condition is true, the non-zero value is transmitted, and the condition is transmitted back when the condition is inexpensive. All process controls must be used to use the Test command to determine the authenticity. The use method of Test command is listed in Appendix B. Test $ # = 0 If there is no parameters in this program, the non-zero value represents "$ # = 0" is true. Conversely, it will return zero. The following introduces various processes: 1. if Then syntax and flow charts are below │ false if (condition) -┐ Then │true │ THEN-COMMANDS TEN-COMMANDS │ FI ---- ┘ │ Condition is a Test command. Condition in the various processes introduced later is Test command.

Diction: Chkarg ----------- ┐ │IF (Test $ #! = 0) │ │ THEN │ │ Echo Arg1: $ 1 │ │FI │ └ --------- - ┘ $ Chkarg Hello Arg1: Hello $ Chkarg $ 2. If THEN ELSE Syntax and Flowcharts │ False IF (Condition) ----- ┐ Then │true │ Then-Commands Ten-Commands else-Commands ELSE ├ -------- ┘ else-commands │ Fi 3. if Ten Elif syntax and flow charts are below │ false ife (condition1) -┐ Then │true │ false Commands1 Commands1 -┐ Elif (CONDition2) │ │ │ THEN │ Commands2 Commands3 Commands2 ├ ----- ┴ ---- ┘ Else │ Commands3 Commands3 Fi Echo 'Word 1: / C' Read Word1 Echo 'Word 2: / C' Read Word2 Echo 'Word 3: / C' Read Word3 IF (TEST "$ Word1" = "$ Word2" -a "$ word2" = "$ word3") THEN Echo 'Match: Words 1, 2, & 3' Elif (TEST " $ Word1 "=" $ word2 ") THEN Echo 'Match: Words 1 & 2' Elif (Test" $ Word1 "=" $ Word3 ") THEN Echo 'Match: Words 1 & 3' Elif (Test" $ Word2 "= "$ Word3") THEN Echo 'Match: Words 2 & 3' Else Echo 'No Match' Fi 4. for in syntax and flow charts are below │ false for var arg-list ┌ - -┐ do │ │true │ Commands │ get a zone from Arg-list │ DONE │Parcrade VAR │ │ │ │ │ CommANDs │ └ ------ ┘ │ ┌ --------- - ┐ ┌ ----- ┘ │for a in xx yy zz │ │ │ │ │ │ │ │ │ └ ----------- ┘ The following: xx yy yy zz 5. For grammar and flow charts are as follows │ false for var ┌ - -┐ do │ │TRUE │ Commands │ get a zone from parameters │ DONE │ Puts to variable var │ │ │ │ CommANDs │ └ ----- ┘ │ Dot Name:

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

New Post(0)