Step by step into the UNIX - UNIX shell program design

zhaozj2021-02-16  190

UNIX shell? Program design

Http://91mail.51.net provides ?? Need a complete PDF document to http://91mail.51.net download ??? Unix? shell overview ??? shell starts and its command??? SHELL command Integration ??? shell variable ??? SHELL control structure ??? SHELL's running environment ???? First section Unix Shell Overview *?? What is shell *?? SHELL Double Features *?? Shell Program language Features *?? SHELL version *?? SHELL version of the choice What is shell

?? The shell is UNIX to provide the user's use interface. The shell is between the kernel and the outer application, and the consistency of coordinating the user and the system, the role of interacting between the user and the system, that is, the shell provides the user with the input command and parameters and obtain the result of command execution. surroundings. SHELL Dual Features? F? Command Interpretation (Command Interpreter) ??????? SHELL Interprets the command line entered by the user, submits the system kernel processing, and returns the result to the user; ??? SHELL is the same as the UNIX command They are all utilities, but differentiated. Once the user is registered to the system, the shell is loaded into memory and runs to the user exit system; and the general command is only loaded by the system only when the system is only called. ? F? Programming language ??? Shell itself is also a programmable programming language. 6.1.1? SHELL features and major versions ????? shell allows comparing complex functionality by programming, but they have different characteristics as language and advanced languages:? Shell is interpretative, most advanced languages ​​are Compiling; • The shell language is different from the object of advanced language processing;? ¸ The shell has a close relationship with the system;? ¹ Shell is easy to write, debug, flexible, but the speed is low;? O shell as a command level language The command combination is very strong. SHELL version? SHELL version selection Select the factor that should be considered: ?? Bourne shell exists on any UNIX system platform, so becoming a standard shell;? · • Bourne shell family has a richer programming language, and C shell family has a simple program interface; ¸? SHELL variant functions are getting stronger, but the more difficult learning and use, so it can be selected according to user programming experience;? ¹ SHELL programming script is personal use or Public, that is, considering the graft problem.

6.1.2? SHELL program 6.1.3 SHELL scripts Establish and execute Section 2 shell variable * ???? shell variable * ???? user custom variable * ???? position variable * ???? Special variables defined * ???? Environmental variable * ???? variable replacement

? F ?? shell is actually a string-based programming language and has variables. The name of the variable must begin with letters or underscores, can include letters, numbers, and underscores. ? F ?? shell variables and can only store text strings, ie it has only one type of variable-string variable. • F?? From the form of assignment, it can be divided into four types of variables or variables. F? Shell has two variables: environment variables and temporary variables.

Environmental variable: permanent variable ???????????? user custom variable ????????????????????? ?? Position parameters

? 6.2.1? User-defined variable F? Syntax Format: Variable Name = String F? When using variables in the program, add a symbol "$" in front of the variable name "=", "=" is not allowed to have blank符; ??? nodehost = "beijing.uucp" ??? Path = / bin: / usr / bin: / etc / bin ??? count = 10? F allows multiple assignment operations, pressing from right to left Cut; ??? # a = $ b? B = abc? C = "ok" ??? # echo $ a $ b $ c ?????????? ABC ABC OK ?? F When a reference When the variable is not set, the implied value is empty; ??? # echo? "$ Mail is path of mailbox" ????????? IS path of mailboxf If you use double quotes "" enclose the value The enclosed strings allow special characters for spaces, tabs, and wraps, and allow variables to be replaced. ??? 1: # mail = / var / mail / fk ???????????? # var = "$ mail is a pats of mailbox" ??????????? ? # echo $ VAR ???????? / var / mail / fk is a path of mailbox ??

Example 2: # Str = "this is / na book" ???????????? # echo $ STR ????????????????????? ??????? a book

? F If you enclose the value with single quotation marks '', the enclosed string allows the special characters of spaces, tabs, and wraps, but does not allow variable replacement. ?? 1? # Book = "english book" ??????????? # msg = '$ book' ?????????? # echo $ msg ??????? ???? $ book ??? ?? case 2 ?? # msg = 'Today is / t sunday' ?????????? # echo $ msg ?????????? Today IS ???????? sunday

• When f references the value of the variable, you can enclose the variable name with the curly bracket {}, so that the variable name is separated from its subsequent characters. If the character followed by the name of the variable name is a letter, a number or underlined, it must To use the curly brackets. ???: # Str = 'this is a string' ?? # echo "$ {str} Ent Test of variables" ?? ?? # echo "$ strong Test of variables"?? Test of variables6.2.2 Enter the output command 1.Read command ??????? You can use the read command by the standard input data, then assign the specified variable format: READ? Variable 1 [Variable 2 ...], for example: read Name ??????????? read ABC?

F If the number of variables is less than the number of data, for example, when performing READ N1 N2, the user enters: first second third

>

Then "first" is assigned to N1, and "Second Third" is assigned to N2.

F If the number of variables is more than data

For example, read? N1? N2? N3, user input: 1 ?? 2

Then N1 value 1, N2 value 2, and N3 is an empty string.

2. Echo command ??????? echo command outputs thereafter on the standard output. Echo parameters can have some special characters? P139 cases: echo "enter the file name -> / c" 2.3? Location parameters ????? position variable as the name referring is a special variable, this is a special variable. The shell process name of the command line is specified as a position variable $ 0, and the parameter is $ 1 .... $ 9. Example: ls ???? / ?? / bin? / Etc? / Usr / bin? / Dev ?? ... ???????????? × ???? × ????? ? × ?????? × ???????? × ???????????? ×? $ 0? $ 1? $ 2 ??? $ 3 ???? $ 4? ??????? $ 5 ??? ... ???? case: # cat? Finduser ??????????????????????????????????????????????????????????????????????? ? #finduser fke Example 6-5 Location parameters Use $ Cat m1.c? main ()? {?? printf ("bebin / n")?} $ cat m2.c? #include

? main ()

? {

PRINTF ("OK / N")

PRINTF ("end / n")

?

$ Cat EX5 # EX5: Shell Sript to Combine Files and count #Lines ????? Cat $ 1 $ 2 $ 3 $ 4 | wc -l # end $ EX5 m1.c m2.c? 10

F You can use the set command to assign a value of the location parameter in the shell program. For example: ??? set m1.c m2.c m3.c assigns a string m1.c to $ 1, the string m2.c assigns $ 2, the string m3.c is assigned to $ 3. But $ 0 cannot be used to assign a value 6-6? Set the location parameter value with SET? $ Cat EX6 ?? Set m1.c m2.c ?? Cat $ 1 $ 2 $ 3 $ 5 | WC-L $ EX6106.2.5? Predefined Special variables have a special variable in the shell, and the variable name and variable value can only be set. ? F? #? - Record to the number of self-variables passing to the shell;? Example 1: # myprog? A? B? C ????????? The value of $ # is 3? Example 2: If? TEST $ #? -lt? 2 ?????????? Then ???? echo? "two or more args required" ???? EXIT ???????? fi?

Example 6-2 $ CAT EX2? IF Test $ # = 0 ?? Then LSELSE ??? for i ????? do ????????? LS -L $ I ?????? DONEFI2 .5? Pre-defined special variable (continued 1) f $? -? Take the exit status after the recent command execution (return ??????? 回): Execute the success return code is 0, the execution is lost ??????? the defeat return code is 1; : # Test? -R? My_file ?? (assuming my_file file is not read) ????? # Echo ?? ??????????? 1f $$?? -? Current process Process number; p142f $! -? Take the last one run in the background (using "&") ????????? process number; ?????????? ?? 2.5? Predefined special variable (continued 2)? F? $ -? -? The SHELL flag, the options used when SHELL starts, or with the options provided by the set command. ? F? $ *? -? Match all position variables; ?? ????????? $ *? ?? match ?? $ 1? $ 2? $ 3? ... ????? "$ *" Match? "$ 1? $ 2? $ 3? ..."? F? $ @? -? Match all position variables; ?? ????????? $ @? Match ??? $ 1? $ 2? $ 3? ... ??????? "$ @" match? "$ 1" "$ 2" "$ 3" ... 6.2.6? Environment variables ?? SHELL execution environment consists of a series of environment variables, these variables are maintained and managed by shell . All of these variables can be reset by the user, and the variable name consists of uppercase letters or numbers. ? Cdpath: Search path to execute when the CD command;? Home: User's Home directory;? IFS ??????: The internal domain separator is generally spaced, tab ??????? ??? ????????????????????? or a wrapper;? Mail ??: ?? Specify the path of a specific file (mailbox), for the mail system;? PATH ?: Find the search path of the command or executable file;? PS1 ??????: ?? The main command prompt, default is "$" or "#" ;? PS2 ??????: ?? Command prompt, default is ">";? Term?: Terminal type used. P1446.2.7 EXPORT statement and environment setting ??????????????? local variable and global variable local variables used in a certain local specific environment. ? F? Register Shell After accepting the command (in-internal command) entered by the user, a child shell is usually derived, and the child shell is responsible for explaining the execution of the command. • F child shell has its own operating environment and variables that can only be used in a specific environment within the scope of the child shell. ?

F child shell cannot access partial variables set by the parent shell, and cannot change the variable value of the parent shell. Example 1: # Cat Vartest1 ??????? echo: $ x: ?????? # x = 100 ???? # VARTEST1 ???? ::? Example 2 :? # cat varteest2? ?? x = 60 ??? echo: $ x: ??? # x = 10 ??? # varTest2 ???: 60: ??? # echo $ x ??? 10 global variable?? Global variable is one Special variables can be referenced by any operational sub-shell. The global variable is defined by the export command, the format is as follows: ?? EXPORT? VARIABLES where variables is a variable table name to become global variable. ? F? Once the variable is defined as a global variable, these are all global variables for all sub-shells;? F? 子 子 s s s s;;? F? If the global variable is changed in the sub-shell The value is actually changing the copy of the global variable, does not affect the global variable; • The use of local variables in the sub-shell takes precedence over the global variable. Example:? # @Var = "global"? # Cat test_var ?? EXPORT G_VAR L_VAR ?? g_var = "sub_shell: g_var" ?? l_var = "sub_shell: l_var"? Echo $ G_VAR $ G_VAR $ l_var ?? # test_var ?? Sub_Shell: g_var sub_shell: l_var ?? # echo $ g_var?: $ l_var: ?? Global ::? #? Local variable and global variable scope? Any variables that have not been defined with the export command It is a local variable, and the child shell cannot deposit a local variable of the father shell; • The global variable of the parent shell can be accessed and modified in the child shell, but this modification has no impact on the parent shell global variable;? The global variable defined by the export command and the modification of this variable have no effect on the parent shell variable;? Schll's Shell;? O You can convert the variable into global variables before and after the value of the variable.

1EXPORT statement ???? p1442. .Profile file ?????? p145 ??? If you modify the .profile file, reset the environment, ie, re-execute the .profile file. $ HOME / .profile3. Environment Variable Settings and Display 4.Set Command ???????????????????????????????????????

Example 6-8 $ CAT EX8? SET -X ?? a = 1; b = 5? Echo "Current Shell Flags $ -"? Echo $ A $ b? Set x? Echo "Current Shell Flags $ -"? SET -V? a = 2; b = 8? echo "Current Shell Flags $ -"? Echo $ A $ b? set v? echo "Current Shell Flags $ -"

$ EX8? A = 1B = 5 Echo Current Shell Flags Xcurrent Shell Flags X Echo 1 51 5 Set X Current Shell Flagsa = 2; B = 8echo "Current Shell Flags $ -" Current Shell Flags Vecho $ A $ B2 8SET Vcurrent Shell Flags6.3 Special Character 3.1 ??? Unix Metacity (Wi-Caracher) definition? * ??? Match any string, including empty strings;?? ? Match any single character; [..., -,!], According to the range "-", list "..." or do not match ?????????????????????????????????????????????? ????? "!" The other formats match the specified character;? / ???? The transaction makes the metamorphism lose its special meaning. ?? case: [AD, X, Y] match characters a, b, c, d, x, y; ???? ?? z * ??? Match any string started with character z;??? ??? x? y ?? Match start with x, end with Y, in the middle is any ?????????????????????????? [! z] matches the Z A single character. 6.3.1? Metacity (continued) Example: [AF] * Match the file name of the character A to the beginning of the character f; ??? ABC ?? D2 ?? E3.c? F.dat?? * Z? Match Any string of character z is ending; ??? Win.z ?? Core.zz ?? a-c_5z ?? rc? .D? Match starts with RC, ends with .d, in the middle is okay ??? Whole characters File name; ??? rc0.d ?? rc2.d? Rcs.d ?????? * [! O]? Matching the file name not ending with O; ???? 6.3.2? Eliminate special The meaning of characters can be used to use freesters, single quotes, and double quotes. F Double Quotation Number ("") Reference? Dual quotation marks can eliminate the meaning of most special characters in double quotes, but cannot eliminate $, `,", /? Four characters of its special meaning: ?? Example: vdate = "` Date` is System Maintenance Day! "F Single Number ('') The meaning of all special characters enclosed in single quotes.? Example: # echo '$ count =' $ COUNT? F? Essential (/) reference? Eliminate the special meaning of a single character behind the escape character.??? # count = `expr Count / * 10` ?? F special string reference Exceptions ???? Both the bid number, single quotes, and transmissions can eliminate the special meaning of the control string (escape characters) for the echo command. These control strings are:? / B? Backspace? / c? After displaying, don't wrap? / f? Show on the beginning of the screen? / n? Renewal? / r? Enter? / T? Table metrics? / V? Vertical tab? /? Anti-flanking 6.3.3? Input, output redirector F? Redirect (re-direction) using standard elevator.????

??

??> ???????????? output change ?????????? >> ????? Add output change ?? f use the standard file description word to redirect ( Reverse)?????? In the UNIX system, use the file description word to define the standard file of the input, output, where:? 0: Standard input ??? 1: Standard output ??? 2: Standard error output 6.3.3? Enter, output redirection (continued 1)? F standard input to reign ???????> ????????: ???????: ???????> end of marker? Example 1: sort

Example 2: Cat < Hellow! ????????????> OK !! ???>! EOF3.3? Input, output redirection (continued 2)? F Standard Output Return (> >>) ???? Command?> File ??? Command? >> File? Example?>? Myfile ??? 2: ls -l? >> ? myfile ??? 3: Cat? file *?>? myfile ???? 4: ps -ef? |? grep? ftp>? myfile6.3.3? input, output redirection (continued 3) ?? f Rational Error Outputs (> >>) ?????? command? 2> file ???? ·? Command? 2 >> File? Example 1: Reduce the error output to the Err_File file. ?? myProg? 2> Err_file ???? 2: Return the standard output and error output to the OUT file. ?? myProg> OUT? 2 >> OUT ?? (two ways equivalents) ?? myprog> out? 2 >> & 1? (renewal processing from left to right) ?? Example 3: Error output is displayed, standard Output rearrange the OUT file. ?????? myprog? 2> & 1? >> out6.3.4? Note, pipeline and background command f Note ??? SHELL program in the front of the front line indicates the concept of notes F? pipeline: pipe: It is a command for the standard output of a command and the standard input of another command. Does not pass any intermediate files; ??? Pipeline line: a command sequence separated by the pipe operator, the simplest pipeline is a simple command ; ????? Piping Operation: Use symbol "|". ??: WHO | WC -L ???? ps -ef | grep ftp ???? ls -lf background command? In the last type of & pipeline (continued 1)? F? Command table concept? Command Table: A string of pipeline (command) constitutes a command table, the simplest command table is a pipeline (ie, a simple command). 3.5 Pipeline (Command) Separator (Command Execute Operator) ??????? Separate the command table element, determine the conditions of the pipeline execution, the meanings:?; Or a newline character?: Indicates the pipeline in order; &&:? Indications according to the conditions (TRUE), executing the latter lines behind;? | | ??: ?? Indicates that the pipeline line behind it is implemented according to the conditions (FALSE); ?? &?: ?? Indicates the front pipe The line is performed in the background (asynchronous).

Pipeline (continued 2)? Example 1: Four pipelines constitute a command table ????? ls? -L ????? Who | wc -l ????? a | B | C | D ????? ps ?? 2: Tiexical 1 equivalent ????? ls -l / bin / usr / bin; WHO | WC -L; A | B | C | D PS? Example 3: SYS_ACCOUNT &? Example 4: Query if the specified file exists, give the corresponding information ???? Test? -F? $ 1 &&? Echo "$ 1 exists" ???? Test? -F? $ 1 ? |? echo "$ 1 NOT EXITS" 6.3.6 Command combination (group command) ?????? Command combination has two forms:? f? {command table} ????? is executed by the current shell Command table. Case: {?? cd? Mydoc; rm junk;} ???? F? (Command table) ???? The current shell is derived from a child shell process to perform the command table. ??: (? CD? Mydoc; rm junk;)? 6.4 Parameter replacement variable Replace this string itself with the execution result of the command? The apart from the single quotes. ? # Now = 'date' ??????? ï? Single number ?? # echo $ now ?? Date ?? # now = `date` ?????????????????????? ? ï? Single chimeker? # echo $ now? 1999 Wednesday, December 8th, 9:51 56 seconds CST? Example 2: # count = 10 ???????????? # count = `EXPR $ count 1` ???????????? # echo $ count ???????????? 11

??? Parameter replacement variable is another way to assign value for variables, its general form: ????????? Variable 2 = $ {variable 1 OP string} ??? shell encountered not set When the variable is used, its value is treated as an empty string. In practical applications, for unset variables, users can use different processing methods as needed, which can be implemented by variables. ??? Variable replacement provides three functions: ?????? Allow replacement of implicit values ​​of the variable;??????? Error information. 6.2.6? Variable replacement (continued 1) 6.2.6? Variable replacement (continued 2)? Example 1: $ {var: -word}? (Suppose $ PARM is not set)? # Echo? "The value of? PARM IS $ {PARM: -UNDEFINED} "? # Echo $ PARM ???? 0 ????????????? [Note]?" 0 "here Empty characters? $? Example 2: $ {var: -word}? (Suppose $ PARM is not set)? # Arg = $ {PARM: - "not defined"} ???? [Note] Double quotes? # Echo '$ arg:' $ arg? $ arg: not defined ?? Section 5 SHELL control structure * ???? IF structure * ???? Condition and test command * ???? case structure * ???? While structure * ???? Until structure * ???? for structure * ???? other command 5.1? IF structure in the cycle body? F? IF simple structure? Format ??? IF test conditions ???? ????????????????????????????????????????????????????????i? ????? Then ??? comMMand2 ??? comMMAND3 ??? ... ??????????? comMMAND4 ???????? ... ?????? fi5.1? IF structure (Continued 2)? F? IF's connection structure? Format 1 ??? if? Test conditions ???????????????????????????????????????????????????? ELSE ??????????? if command2 ?????????????????????????: ?? ????: ??????????? fi ??????? fi5.1? IF structure (continued 3)? F? IF connection structure? Format 2 ??? if? test Condition ?????????????????????? Elif command2 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?? Commands ?????????????????: ????????????????????? ?????: ??????????? comMMands ???????? Else ?????????????????????? fi •? The condition test can be judged using the general command to perform success or not. P161

5.2? Conditions and Test Commands? F? Simple Conditions ??? Decision Conditions in Advanced Language Depending on the result of the operation, while the shell language dependent condition is the "export status" executed by the command. ??? "Export status" ($?): ?????????? Success: 0?, True ?????????? Failure: x?, False ??? (x is a non-0 value) Example: Judgment the specified directory exists and displays the corresponding information. #? cat? Checkdir? Test -d $ 1 && Echo "$ 1 IS A DICTORY" && EXIT 0? Echo "$ 1 is not a dictroy"? EXIT 15.2 • Conditions and Test commands? F? Test command ????? Test command Can be used for strings, integers, and files. Its command format is as follows: ?? Test? Expression ????????? Note? [] Space in [] EXPRESSION is the condition of the test, calculation results : ?????????? is true, return "zero" exit status, ?????????? is false, otherwise returns "non-zero" outlet status. ? Case: Decades whether the number of users currently is more than 10? ????????????? #? test "` WHO | WC -L `" -gt 10 ?????????? # echo $? 5.2? Conditions and Test commands? F? File test expression in TEST? 5.2? Conditions and Test commands? Example 1: Check if the specified file exists and read? Test? -r? / usr / fk / message? example 2: Check if the specified file is Catalog? Test? -D? / Usr / src / local / sendmail? Example 3: Check if the specified error file is empty, if it is not allowed? "The content of the file is listed. ? Test? -s? $ ERRFILE? &&? Cat? $ Errfile 5.2 • Conditions and Test Commands? F? Test string test expressions?? 5.2? Conditions and test commands? Example 1: Two strings compare? # User = smith? # Test "$ user" ?? =? Smith? # echo $ ?? 0? Example 2: Find the specified file or directory ???? # Cat Search ????? Test "$ 1" || {Echo "Err: no parameter";? / ?????????????????????????????????????????????????????????????????????????????????????????????????? 5.2? Conditions and Test Command? Example 2: String with spaces Compare? # Month = "january?" ????????????? # Test "$ month" =? January? # Echo $ ?? 1? # test $ month? =? January? # echo $ ?? 0? Difference: SHELL When processing the variable, there is a double quotation number will retain its content, and the space is filtered when the double quotation is omitted. .

5.2? Conditions and test commands? Example 3: String strings with spaces Compare? # A = "Testing string"? # Test "$ a"? =? "Testing string"? # Echo $ ?? 0? # Test $ a? =? "Testing string"? Test: unknown operator string

??? When the shell processing the variable $ A, the variable is replaced, and the testing string is passed to TEST, and TEST handles String as operators, so error. 5.2? Conditions and Test Commands? Example 4: With an empty string (or not set string comparison)? # Name = "" # test "$ name"? =? Smith? # Echo $ ?? 1? # Test $ name? =? smith? test: argument expected ??? SHELL handles the variable "$ name", the double quotation number will be reserved as a "position holder" and pass the value to TEST. Ensure the normal execution of the processing. 5.2 Conditions and Test Commands? Example 4: String strings with an empty string compare? # Blanks = "????"? # Test $ blanks? # Echo $ ?? 1? # Test "$ blanks"? # Echo $ ?? 0 ???? When the shell processing the variable $ blacks, filter the space off and make it change to a null string to Test; and the double quotes retain the position of the "position holder", its value is a space (blank character) ), Pass it to TEST. 5.2? Conditions and test commands? Example 4: String with operator compare? # Symvar = "="? # Test-z "$ symvar"? Test: argument expected ?????? Error reason is " = "The operator is higher than the priority of the" -z "operator, so the test command expects to have an argument after the equation. In order to avoid the above problems, use the following command to replace: # Test x "$ symvar" =? X? # Echo $ ?? 15.2? Conditions and test commands? F? Test command can be used for integer comparison ?? First of all Two concepts of clear integer comparison: ???? Shell does not distinguish the type of value in the shell variable, in terms of the variable itself, it is only a set of strings, both only one type of variable - string variable. ??? • When using an integer comparison operator, it is the Test command to explain the integer value stored in the variable, not the shell. ? 5.2? Conditions and test commands (continued 12)? F? Test integer test expression ??? 5.2? Conditions and test commands (Continued 13)? Example? # X1 = "005" # x2 = "10"? # test "$ x1" = 5 ????????????????? # echo $ ?? 1? # test "$ x1" -eq 5 ?????? ????? # e $ ?? 0? # test "$ x2"? -eq 10? # echo $ ?? 0 ????? 6.5.2? Conditions and Test command? f • The logical operation of the expression ??? logical operator includes:?! ?? - ?? Logic non-single operator, can be placed before any other Test expression, the non-value of the expression operation result. ? -a ?? - ?? Logic and operators, perform logic and operations of two expressions, and return true values ​​only when both are true. ? -o? - ??? Logic or operator, execute two logical or calculations of two expressions, and return true values ​​only when one is true.

5.2 Conditions and Test Commands? F? The priority of the logical operator ??? Logic operator priority (from high to low) arrangement? As follows: ???? ()? ???! ??? ??? -a ???? ???? -o? ?? The logical operator priority is lower than the character string operator, the digital comparison operator, and the priority of the file operator. 6.5.2 • Conditions and Test commands? F? Expression logical combination? 6.5.2 • Conditions and TEST commands? Example 1: When the specified file is not read, it is true. ? Test! -r / usr / fk / message? Example 2: When the specified file exists, and Message is readable, when the file specified by the $ mailfile is true, it returns true. ? Test? -r / usr / fk / message? -a? -f? "$ mailfile"? Example 3: When the variable value is equal to 0 and less than 10 is true. Test? "$ count"? -ge 0? -a? "$ count"? -lt 10 ??? 4: ??? Test? / ("$ a" -eq 0? -o? "$ b "? -gt 5 /) ?? -a?" $ c "? -LE 86.5.3? case structure ?? format ???? case value in ?????? pattern1) ??? comMmanD11????? ????????????????????????????????????????????????????? ?????????? ... ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ? ... ???????????????????????????????????????????????????????????????????????????????????????????????????? ????????????????????????????????????? ESAC ????? 5.4? While structure ?? format??? ? While command ???? do ??? comMMAND ???????? ...? ?????????????? Until Command ???? do? ?? Command ???????? ... ??? command ????? done5.6? for structure ?????? for? variable in arg1 arg2 ... argn ???? do ??? Command ???????? ... ??? command ????? DONE5.7? BREAK command and continue command? F? BREAK command ?? Break is the internal command of the shell, used in the cyclic body according to the command Running return condition, directly terminate execution of commands within the cyclic body. When executing the BREAK command, the control flow is transferred from the first command from the cyclic body to DONE. 5.7 BREAK command and continue command? F? Continue command ?? Continue is the internal command of the shell, and directly enters the next loop command directly according to the return condition running in the cyclic body. ?? When the Continue command is executed, the control stream is directly transferred to the first command in this cycle.

5.10? Function definition and use ?? function format ?? functionName () {?????? command ????????????????? Definition and use? ?? do ?????? ie called restdata ?????? case? "$ reply" in ??????????? [yy])? RETURN 0 ;; ????? ?????? [nn]) ??? Return 1 ;; ??? * ????) ??? echo "please enter y or n!"> & 2 ;; ?????? eSAC? ??? DONE ?? execution? # getyesorno "do you wish to contact" || EXIT? Show? Do you wish to continche (y / n)? Seventh chapter SHELL running environment * ???? local variable *? ??? The application of the shell program * ???? Global variable * ???? Local variable and global variable scope * ???? program debug? 7.4? Program debugging ??????? shell provides a variety of Tools to use when debugging the shell program, which allows you to observe the execution of a shell program. ?????? Commonly used test methods are: ?????????? SHELL program detailed tracking; ????????????? SHELL program tracking execution;? 7.4? Program Debugging (continued 1)? F? SHELL program detailed tracking feature allows the user to observe the read and execution of a shell program, if the syntax error is found when reading the command line, the execution of the process . After the command line is read, the shell displays the command line in the standard error output in the form of reading, and then executes the command line. There are two ways to perform the execution of the shell program in detail: ?? The detailed tracking of the entire program and the detailed tracking of local programs.

7.4? Program debugging (continued 2) ?? The tracking of the entire program is executed ???? ?? sh? -V? Shprog ?? is used to track scripts for the entire file. • • Local program tracking? Format ???? ?? set ?? -v ???? -? Set the tracking mark ?? set? V ???? - • Close the tracking mark? To implement some scripts in the file for tracking. ? 7.4? Program debugging (continued 3)? Example:? # Cat traced ??? Date ??? echo $ PATH | WC -C ??? # traced ??? November 05, 1998 17:29 59 Second CST ??? ??? 45 ??? # sh -v traced ??? Date ??? November 05, 1998 At 17:30 08 seconds CST ??? Echo $ PATH | WC -C ?? ?? ?? 45 ????????????????????????????????????????????????????????????????????????????????? 7.4? Program debugging (continued 4)? F? SHELL program tracking execution ??? SHELL tracking execution function allows The user observes the execution of a shell program, which makes the command line after performing all the replaces before execution, displaying each replaced command line in the standard error output, and adds prefix symbol " " before row (but variable assignment) The statement does not add " " symbol), then execute the command. In the same detailed tracking, there are two ways to perform tracking execution of the shell program: ?? The tracking execution of the entire program and the tracking of local programs. 7.4? Program debugging (continued 5) ?? The tracking execution of the entire program? Format ???? ?? sh? -X? Shprog? Is used to implement tracking of the entire file script. • • Local program tracking execution? Format ???? ?? set ?? -x ???? -? Set the tracking mark ?? set? X ???? - • Close the tracking mark?? To implement tracking execution of partial scripts in the file. ? 7.4? Program debugging (continued 6)? Example: ???? # Cat traced ???? Date ???? Echo $ PATH | WC -C ???? # sh -x traced ???? Date ???? November 05, 1998, Thursday, 17:30, 08 seconds, CST ???? Echo? / Bin: / usr / bin: / usr / fk / bin ????? / bin: / usr / bin: / usr / fk / bin ????? wc? -c ??????????????????????? 7.4 • Program debugging (continued 7)? F? Detailed tracking and tracking execution combination ???? The entire program is executed ???? format ???? ?? sh? -Vx? Shprog ?? · Local program Tracking execution ???? format ???? ?? set ?? -vx ???? -? Set the tracking mark ?? set? Vx ???? - • Close the trace mark 7.5? SHELL program application • Definition of the shell program ??? If a file contains a string of simple command strings, it is called a command file; if a file contains a more complex command combination (often used the shell condition command, control The structure and other advanced features are called the shell program (or the shell process). Normally, the name of both did not distinguish. ???? The basic operation in the shell programming language is the UNIX system command, which is the most angry part in the UNIX system, and is extremely important for the use of UNIX systems.

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

New Post(0)