SHELL entry foundation
Time: 2004/01/01 Source:
LinuxSir
Code: 1. Establish and run the shell program What is the shell program? Simple SHELL program is a file containing several rows shell or Linux commands. Like the program to write advanced languages, write a shell program that requires a text editor. For example, if you have a text editing environment, enter some shell / linux command line, enter some shell / linux command line, and form a complete program file. Execute the shell program file with three methods (1) #CHMOD X File (2) # SH File (3) #. File When writing shells, the first line must specify the system to explain your shell programs, such as: #! / bin / bash, #! / bin / csh, / bin / tcsh And #! / Bin / pdksh. 2. SHELL Variable (1) Common system variable $ #: Save the number of program command line parameters $?: Save the back code of the previous command $ 0: Save program name $ *: Save all the input command line parameters $ @: ("$ 1" "$ 2" ...) in the form of ("$ 1" "$ 2" ...) Save All Entered command line parameters (2) Define variable shell language It is a non-type interpretation language. It is not like to declare the variable in advance when programming with C / Java language. Give a variable, it is actually defined the variable. In all shells supported by Linux, you can use assignment symbol (=) as Variable assignment. Such as: ABC = 9 (Bash / PDKSH cannot leave space on both sides) SET ABC = 9 (TCSH / CSH) Since the variable of the shell program is not type, the user can store the same variable When the character is stored, the integer is stored. Such as: Name = ABC (Bash / PDKSH) SET NAME = ABC (TCSH) After the variable assignment, simply add a $ to reference in front of the variable. Such as: Echo $ ABC (3) position variable When run When a shell program that supports multiple command line parameters, the values of these variables are stored in the position variables, respectively. The first parameter is stored in the position variable 1, and the second parameter is stored in the position variable 2, and push it according to the class ... SHELL retains these variables, not allowing users to define them in a way. The same variables are quoted by the $ symbol. 3. SHELL Using the number of quotes Shell Use quotation marks (single number / double quotation marks) and antilactors ("/") Used to The shell interpreter shields some special characters. The reverse quotation marks (") have special significance to Shell. Such as: abc =" how are you "(bash / pdksh) set abc =" how are you "(tcsh) this command line 3 String of words HOW Are You as a whole assignment to variable abc. Abc1 = '@ logname, how are you!' (Bash / PDKSH) set abc1 = '$ logname, how are you!' (Tcsh) ABC2 = "$ Logname, how are you!" (Bash / PDKSH) set abc2 = "$ logname, how are you!" (Tcsh) logname variable is the shell variable for saving the current user name, assuming his current value is: Wang. Execution After two commands, the content of ABC1 is: $ logname, how are you !. And the content of ABC2 is; WANG, How are you !. Like single quotes, the anti-slope can block all special characters. But he once You can only shield a character. You cannot block a set of characters. The function of the reverse number is different from the above three symbols. He does not have the function of blocking special characters. But you can pass the running results of a command to another command. Such as: contents = `ls`
(Bash / PDKSH) SET Contents = `LS` (TCSH) 4. The test command in the SHELL program is used in Bash / PDKSH, the command TEST is used to calculate the value of a conditional expression. They often have been in conditional statements and cyclic statements It is used to determine whether certain conditions are met. The syntax format of the test command: Test Expression or [Expression] In the Test command, you can use a lot of SHELL internal operators. These operators are described below: (1) String operator for Calculate string express Test command | meaning --------------------------------------- STR1 = STR2 | When STR1 is the same as STR2, returns True Str1! = STR2 | When STR1 is different from STR2, return True Str | When Str is not empty character, return TRUE -N STR | When STR is greater than 0, Return true -z str | When the length of Str is 0, return true ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------ (2) Integer operator has functions similar to character operators. Just their operation is for integer Test expressions | meaning ---------------- ---------------------------- INT1-EQ INT2 | When INT1 is equal to INT2, return TRUE INT1 -GE INT2 | When INT1 is greater than / Oriented when INT2, return True INT1 -LE INT2 | When INT1 is less than / equal to INT2, return TRUE INT1 -GT INT2 | When INT1 is greater than INT2, return TRUE INT1 -NE INT2 | When INT1 does not equal inT2, return TRUE - -------------------------------------- (3) Operators for file operations They can check: Whether the file exists, the file type and other TEST expressions | Meaning ------------------------------- --------------- -d file | When File is a directory, return true -f file | When File is a normal file, return TRUE -r file | Returns true -s file | When the File file is greater than 0, return true -w file | When File is a writable file, return true -x file | When File Is a executable file, return TRUE --------------------------------------- ------ (4) SHELL logical operator for modification / connection contains integers, strings, file operator express TEST expression | meaning ------------- -------------------------------------------! EXPR | When EXPR When the value is false, return true expr1 -a expr2 | When EXPR1, EXPR2 value is TRUE, return TRUE EXPR1 -O EXPR2 | When EXPR1, EXPR2 value is at least one True, return TRUE ----- -------------------------------------------------- ---- Note: TCSH shell does not use Test commands, but the expression in TCSH can also bear the same function. The expression of TCSH support is the same as the expression in C. Usually in the IF and While commands. TCSH Expression | Meaning ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------- INT1 <= INT2 | When INT1 is less than / equal to INT2, return TRUE INT1> =
INT2 | When INT1 is greater than / equal to INT2, return TRUE INT1
End. 6. Circular statement When you need to repeat some operations, you will use a loop statement. (1) FOR statement knows that in many programming languages is the most common. It is no exception in the shell .for statement Require shell to continuously perform a certain number of times in a set of commands in this statement. Syntax format: Bash / PDKSH Usage 1: for var1 in list do commands Done meaning: In this For statement, each value corresponds to each value in List The shell will execute a set of commands representing a Commands representative. In each execution of the entire loop, the variable var1 will rely on the different values in the list. Usage 2: for var1 do setatements Done meaning: In this For statement, SHELL performs a set of commands for STATEMENTS representatives for each of the variable var1. When using this form of statement, the shell considers that all position variables are included in the VAR1 variable, and the program line is placed in the position variable. Parameter value. That is to say, he is equivalent to the following form: for var1 in "$ @" Do Statements Done TCSH Usage: Does not for this word in TCSH, with the for statement FORECH NAME (List) Commands End Example: for File; Bash / Pdksh Do Tr AZ Az <$ file> file.caps done #; tcsh foreach file ($ *) TR AZ AZ <$ file> $ file.caps end (2) While statement while statement It is another loop statement provided by Shell. The While statement specifies an expression and a set of commands. This statement makes the shell repeatedly execute a set of commands until the value of the expression is false. Syntax format: While Expression; Bash Do Statements Done While (Expression); TCSH Statements End Example: count = 1; Bash While [-n "$ *"] *** do echo "this is a parameter number $ count $ 1" Shift count = 'expr $ count 1' Done set count = 1; tcsh while ("$ *"! = ") echo" this is a parameter number $ count $ 1 "Shift Set count = 'expr $ count 1' end statement The function of the Shift command is to pass all command line parameters. (3) Until statement until and while statement has similar syntax formats and features, different is in the While When the value of Expression is True, the shell executes the command group; when the expression of Expression is false, the shell executes the set of commands. Synti-format: untric Expression Do Commands Done Example: count = 1 Until [-z "$ * "] *** echo" this is a parameter number $ count $ 1 "shift count = 'expr $ count 1'
Done loves to bring *** in the above example. Expression in While: -N String, his meaning is true when String is not a null string; expression in unsil: - Z string, his meaning is that when String is an empty string, the expression value is TRUE. It can be seen that the two programs are set to the conditional expression. (4) SHIFT statement Bash and TCSH support Shift command. SHIFT will store the command line parameters in the position variable, transmit it to the left. For example, the current value of the position variable is: $ 1 = file1 $ 2 = file2 $ 3 = file3 After executing the Shift command, the value of the position variable is: $ 1 = File2 $ 2 = file3 can also specify the number of position variable transfer in the Shift command, such as Shift N example: while ["$ 1"] do if ["$ 1" = "- i"] Then infile = "$ 2" Shift 2 Else IF ["$ 1" = "- o"] Then Outfile = "$ 2" Shift 2 Else Echo "Program $ 0 Does Not Recognize Option $ 1" Fi Done TR AZ AZ <$ INFILE> $ OTFILE (5) SELECT statement SELECT statement is PDKSH A unique loop statement. He is different from the circulatory statement introduced. He does not repeatedly calculate a conditional expression and decides whether to execute a set of commands in accordance with the value of the expression. The functionality of SELECT is automatically generated a simple text. Menu. Syntax format: SELECT MENU [IN list_of_Items] Do Commands Done meaning: When a SELECT statement is executed, PDKSH creates a menu option for members of each column in list_of_items .list_of_items can be a variable containing multiple options It can also be a set of options directly listed in the program. If there is no list_of_items in the statement, the SELECT statement will use the location variable as list_of_items. Example: Select Menuitem in Pick1 Pick2 Pick3 Do Echo "Are you Sure You want to pick $ menuitem "Read res" Receive the user's input, and store the input value in a specific variable. IF [$ res = "y" -o $ res = "y"] Then Break; used to exit while , For, SELECT, etc., the REPEAT statement, the REPEAT statement is the unique loop statement provided by TCSH. Use the repeat command to ask the shell to perform a certain number of times for a command. Syntax format: Repeat Count Command, etc.; Foreach Num $ *) REPEAT $ Num echo -n "*" echo ""
End 7.Shell's function shell allows users to define their own functions. The function is an important structure in the advanced language. The function in the SHELL is the same as the function defined in the C or other languages. Starting from the beginning, one line is written The main benefits of using the function is conducive to the organization of the entire program. In Bash, the syntax format of a function is as follows: fname () {shell comance} Defines the function, you need to call them in the program. Bash 's format: FName [PARM1 PARM2 PARM3 ...] When calling a function, you can pass any multiple parameters to the function. The function will see these parameters as the location variable of his command line parameters. Example: This program defines 4 functions: Upper : Converting the letters passing into his file to uppercase, and store the end of the same name for .Out. Lower (): Converts the letters passing to his file into lowercase, and stored the same name The end of the .Out (): The output is passed to the content of the file. USAGE_ERROR (): Output help information. The master module of the program is a case condition statement, and he according to the first parameters in the command line, Decide the function to complete, and call the corresponding function to complete this function. Upper () {Shift for i do triaa az <$!> $ 1.out RM $ 1 MV $ 1.out $ 1 Shift Done;}} Shift for i do tr Az Az <$ 1> $ 1.out RM $ 1 MV $ 1.out $ 1 Shift Done;} print () {Shift for i do lpr $ 1 Shift Done;} $ 1 SYNTAX IS $ 1 "echo "Echo" Where option is one of the fol line "echo" p - to print frame file "echo" u - to save as uppercase "echo" l - to save as lowercase ";} Case $ 1 in P | -P) Print $ @ ;; u | -u) Upper $ @ ;; l | -l) Lower $ @ ;; * USAGE_ERROR $ 0 ;;