Shell Script mode

Source: Internet
Author: User
Tags arithmetic

"No rules, inadequate surrounding area." Here are some of the fixed patterns of shell scripts. first, the basic rules of the script1, first say the file name concise as far as possible to see the first digit prohibition of digital2. The file name should end with ". Sh" (Note that there is a point)3. The first line of the script must include the shell life sequence "#! "Example" #! /bin/bash "gives execution permission to specify the absolute path or relative path of the script on the command line (the first line can be written and not written but as a literate ops man must write!!) )4, then began to write comments: file name, function description, author and contact information, last modified date, version number and some instructions.   second, the script programming logic processing Way1. Sequential execution2. Cyclic execution3. Select Execution third, the next step is the process of heap command1, the variable name cannot use the reserved word in the program: example if, for. You can use only alphanumeric and underscores, and you can't start with numbers.2, Position variable (this is a very convenient skill)$, $, $, $ ... $#file namePass the Tertiary. A parameterthe number of arguments passed to the script$* [email protected]all parameters passed to the script, all parameters are combined into a stringall parameters passed to the script, each argument being a standalone stringNote: "$*" and "[e-mail protected]" are not easy to distinguish between the difference only when the double quotation marks are wrapped up in time to see the differenceSetClear all positional variables3. Arithmetic arithmeticSeveral operational symbols: + 、-、 *,/,% modulus (take-up), * * exponentiation (some scenarios need to be escaped) two arithmetic examples fixed format * Example: Let r=9+9 view the results of the operation Echo $r * Example: r=$ ((9+9)) View the results of the operation Echo $r $RANDOM randomly generated A string of numbers (0-32767) * Example echo $[$RANDOM%50] Take any one number between 0-494, Enhanced type assignmentEnhanced operation symbol + =,-=, *=,/=,%=* example let count+=3 add 3 after the assignment * example let var++ default self-add 15. Bash test type String Test-GT: is greater than = =; is equal to-ge: is greater than or equal to >: ASCII is greater than Asscii-eq: is equal to <: small In-ne: Not Equal! =: is not equal to-LT: is less than =~: whether the left string can be matched by the pattern on the right-le: is less than or equal to-Z: Test string is empty, not empty is false, NULL is true-N: Test string is Not empty, not empty is true, empty is false6. Use the Read command to accept the input-P: Output is displayed to screen-s: Silent Output (common password, etc.) Example: Reda-p "enteer a filename:" File Note file must be preceded by a space7. String SlicingFirst set a variable myname= "/usr/sbin/ntpdate"* String length testecho ${#maname}1after viewing the set string length, a total of 17 "/" will be counted* Variable advanced operationExample: Echo ${myname#/*/}sbin/ntpdate* A "#" number is deleted by the front comparison to the shortestExample: Echo ${myname##/*/}ntpdate* Two "#" numbers are deleted by the front comparison to the longestExample: Echo ${myname%/*}/usr/sbin * A "%" is deleted by the back comparison to the shortestExample: Echo ${myname%%/*}(empty)two "%" by the back contrast delete the longest match to the8. Process Control (if, for, while)**IF statement format and examplesif (Command1 condition 1); thenIf condition 1 executes the failed execution condition 2 cases (1<1) is wrong(If-flase failed) Execute actionElif (Command2 condition 2)Condition 2 Execution Failure Example (1>1)(if-flase)Perform ActionsElse(The direct execution condition 3 can be used without the execution condition example (1=1)(If-true Success)Perform ActionsFi(Terminator)**for statement format and examplesFor i in {1..10};d oset the variable equal to 1~10 for I (remember "I" Cannot add $ symbol)Useradd User${i}created 10 users user1, User2, User3 .....Done(Terminator) **while statement format and examplesWhile True;doif the condition is true continue executionecho {1..100}show 1~100 repeat loop execution unless manually stoppedDoneEnd Character Iv. Summary Tips* If the command is too long can use "\" branch to write *shell script does not require indentation, but to form a good habit of indentation * can be defined environment variables specifically written to a file, avoid modifying system files * Key operations must have log output, specifically record the success or failure of the operation and the time of execution * It is likely that the script will contain sensitive information, and if it is made public, be sure to confirm that it has been deleted * Try to give each statement or code execution to an execution result status * Recommended for the start of script execution/etc/profile recommend using source or "." * The variable or file name defined in the script as far as possible use "camel writing" to see the meaning of the name to avoid the use of a, B, c similar definitions * Another script has three skills to write more practice, write more practice, write more practice, this.

Shell Script mode

Related Article

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.