Linux script file generation

Source: Internet
Author: User
I. Generation of script files #! /Bin/bash Operation Command # Note   A , Script file permissions, the script file must be executable Limits. # Chmod 755 Script File Name 

BThe path of the script file must be stored in$ PathPath. If the path does not existExecution method :#./Script File Name

 

String replacement command 1. Tr, which is often used in pipeline operations. Replaces the specified string of the output content. # Command | tr 'original string' 'replace string' # Command | tr-D 'string to be deleted '-D Delete string 2. sort by specific strings # Sort [-tkn] File Name -T: Set the field delimiter. -K: Set the selected field -N: use numbers for sorting 

Example: # Cat/etc/passwd | sort-T:-K 3-N

 

3. Filter duplicate data-uniq Uniq is used to filter adjacent and repeated data in the output content, and only one of them is retained. # Command | uniq # Command | uniq-C collects statistics on the rows found If more than 10 bash logs are logged on, change the first 10 logon shells to CSH. 

4. Double orientation-teeSyntax: Tee redirects the standard output of the previous file to the file and passes the standard output to the next command. Example: # Cut-D:-F 7/etc/passwd | tee file1 | cat file15, xargs: The command output after the first command is handed over to xargs for processing. Example: ls a * | tee file1 | xargs Rm-T Example 2: The file deletion prompt read-P "Enter the file you want to delete:"; ls $ A | tee file1 | xargs Rm-f | echo 'cut file1' has been deleted | echo ''> file1

Ii. Structured control statements -- Conditional Control statement 1 , If -- then-- Fi Syntax: If Command (if the return value is 0, the command after then is executed; otherwise, the command after Fi is executed) Example: Determine whether a file exists #! /Bin/bash read-P "Enter the file you want to search for:" A if ls $ A then Echo "this file exists" Exit 0 fi Exit 5

Example 2: Determine whether a user exists #! /Bin/bash read-P "Enter the user account you want to search for:" A if grep ^ $ A/etc/passwd then Echo "this user exists" Exit 0 fi exit 4

2 , If-then -- else-- Fi Syntax: If Command (if the return value of the command is 0, the command after then is executed; otherwise, the command after else is executed) Example: File Creation script #! /Bin/bash read-P "Enter the file you want to create:" A if ls $ A then Echo "this file already exists" else touch $ a fi

3 , Test JudgmentSyntax: Test judgment condition (if the condition is true, 0 is returned; otherwise, 0 is returned) Judgment type: N1-ge N2; N1> = n2 N1-Le N2; n1 <= n2 N1-ne N2; N1 is not equal! = N2 N1-GT N2; N1> N2 N1-lt N2; N1 <N2 example :#! /Bin/bash if Test 6-GT 7 then Echo "OK" else echo "error" Note: In if, replace test with the [] Syntax: if [judgment condition] The statement meets the check condition Syntax: If [judgment condition] & [judgment condition] File comparison-D file; check whether the file exists and is a directory-e file; check whether the file or directory exists (easy to use)-f file; check whether the file exists, it is also a file-r file; check whether the file exists and whether it is readable-s file; check whether the file exists and whether it is not empty-W file; check whether the file exists, whether the file can be written to-X file; check whether the file exists and whether it can be executed

For example, if the input score is below 60, the score is 60-60 ~ The score is between 80 and 80 ~ Good between 90, 90 ~ 100 excellent, less than 0 or more nonsense ;#! /Bin/bash read-P "Enter the score: "A Elif [$ A-lt 60] & [$ A-GE 0] Then ECHO" fail "Elif [$ A-ge 60] & [$ A-le 80] Then ECHO "pass" Elif [$ A-ge 81] & [$ A-Le 90] Then ECHO "good" Elif [$ A-ge 91] & [$ A-Le 100] Then ECHO "excellent" FI injection l sealing rules, if there are a few, you need to add a few fi later. Except for Elif, it is OK to use one for sealing)

If New Feature Syntax: If (( Numeric expression )) 

Example: IP type identification script

#! /Bin/bash a = 'ifconfig eth0 | head-N 2 | tail-N 1 | cut-D: -F2 | cut-D ''-F 1' if ($ A >=192 & $ A <= 223 )) then ECHO "Class c ip" Elif ($ A <= 191 & $ A> = 127) Then ECHO "Class B IP "............ Fi

III, List Definition -CaseSyntax: Case variable name in variable value 1 | variable value 2) command 1 ...... Variable value n | variable value n *) command n esac

Example :#! /Bin/bash read-P "Where are you from:" A case $ A in Beijing | Shanghai) echo "you are in room 502"; Shanxi | Hebei) echo "You live in 505"; Guangdong | Henan) echo "You live in Room 510" esac

Iv. Cyclic commands -For is used to create a Value List for repeated loops. Each loop uses a Value Syntax of the Value List: for variable name in variable value list do command 1 ...... Command n done

For example, determine which files are under the root directory and which directories are #! /Bin/bash for file in/boot/* do if [-d $ file] Then ECHO "$ file is a directary" Elif [-F $ file] Then ECHO "is File FI done

C-type for LoopSyntax: For (variable name = variable value; matching condition; variable operation) do command 1 done example: print the number of 100 #! /Bin/bash for (a = 0; A <= 100; A ++) do echo $ A done Note: a = 0 (define the variable) A <= 100 (Execution condition of the loop body) A ++ (variable operation)

Nested loop ofFor example, print the 9-9 multiplication table #! /Bin/bash for (L = 1; L <= 9; L ++ )) do echo "$ L * $ r = 'echo $ [$ L * $ R] '" done Example 2. Print the triangle #! /Bin/bash for (H = 1; H <= 10; H ++) do for (Sn = 1; sh * 2-1> = Sn; SN ++) do S = "$ s 'echo 0'" done echo $ S = Done

V, While LoopSyntax: While test (execute the loop body when the return value of the test command is "0") The do command donewhile statement allows multiple commands to be defined. Execute these commands in each loop :#! /Bin/bash var1 = 10 while echo $ var1 [$ var1-GE 0] Do echo "This is" var1 = $ [$ var1-1] Done

6. Until LoopSyntax: Until test (execute the loop body when the return value of the test command is not 0) Do Command done example: print 100 #! /Bin/bash var1 = 100 until [$ var1-EQ 0] Do echo $ var1 var1 = $ [$ var1-1] Done

Shell script exampleExample 1: The while loop body outputs users in the system in a specific format. The first user is root, and the second user is ...... #! /Bin/bash while [$ n-LE 1] Do for test in 'cut-D:-F1/etc/passwd' do echo "the number of $ n users is: "$ Test N = $ [$ n + 1] Done done

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.