Bash Script Programming Knowledge reserve

Source: Internet
Author: User

Bash Script Programming:

???? Scripting: Interpreter interpretation execution;

  1. first of all have to clear up some trivial knowledge points, I try to do what I learned to help friends comb
      1. Programming Environment: (I will be in the next chapter, graphic examples three combinations way to take everyone to study together)

        ???????????? Process Control statements;

        ???????????????? sequential execution;

        ???????????? Cyclic execution;

    ???????????????? Select execution;

    2.???????? Bash variable is weak type; default character type;

    ?

    ???????? Variable reference: ${var_name}

    ???????? Quotes:

    ???????????? Weak references: ""//can implement variable substitution

    ???????????? Strong reference: "//non-complete variable substitution

    ???????????? Command reference: '

    3.

    ???????? Declare a variable to be an integer variable:

    ???????????? Let Var_name=value

    ???????????? Declare-i Var_name=value

    such as: The script wants to declare that the variable sum is an integer and assigns it a value of 0

    ???????????????? Declare-i sum=0

    ?

    ???????? Declare a variable to be an environment variable:

    ???????????? Export Var_name=value

    ???????????? Declare-x Var_name=value

    Such as:

    ???????????? Export-i sum=0

    4. Script writing format:

    ???? First line: stating the Interpreter; #!/bin/bash

    ???? Comment lines: All lines that begin with # are comment lines, which are ignored by the interpreter;

    ?

    ???? Execute script:

    ???????? Give execute permission; specify path execution;

    ???????? Direct pass script to bash interpreter

    ?

    ???????? Bash options:

    ???????????? -N: A syntax error in the test script;

    ???????????? -X: Debug execution;

    5. Arithmetic operations:

    ???????? $[expression]

    ???????? Let Var_name=expression

    ???????? $ ((EXPRESSION))

    ???????? $ (Expr argu1 argu2 argu3)

    ???? 5.1 Cases a=1,b=2 seek a+b?

    ?

    ???? The first method:

    ???????? [Email protected] ~]# a=1

    [Email protected] ~]# b=2

    [[email protected] ~]# echo $[a+b]

    3

???????? The second method: the use of the Let command

???????????????????? Format:

???????????????????? Let assignment expression

???????????????? "Note" Let assignment expression function is equivalent to: ((Assignment expression))

???????????? 5.2???? Example 1: Add 5 to the argument I

???????????????????????? [Email protected] ~]$ i=2

[[email protected] ~]$ let i=i+5

[Email protected] ~]$ echo $i

7

==> Remove Let definition

[Email protected] ~]$ i=i+5

[Email protected] ~]$ echo $i

I+5

Hint: let i=i+5 equals ((i=1+5)), but the latter is more efficient

???????????? 5.3???? Example 2: Small project that uses let count to monitor the status of a Web service (if you can't read it, you can skip it, this is just a lift)

???? #########################################################################

# File Name: _server_monitor.sh

# Author:changsheng

# Mail: [email protected]

# Created Time:fri 03:48:07 PM CST

#########################################################################

#!/bin/bash

#监控服务状态

Servermonitor () {

???? #服务状态监控

???? timeout=10

???? Fails=0

???? Success=0

???? While True

???? Do

???? /usr/bin/wget--timeout= $timeout--tries-1 http://172.16.0.1/-q-o/dev/null

???????? If [$?-ne 0]

???????? Then

???????????? Let Fails=fails+1

???????????? Success=0

???????? Else

???????????? Fails=0

???????????? Let Success=1

???????? Fi

???????? If [$success-ge 1]

???????? Then

???????????? Exit 0

???????? Fi

???????? If [$fails-ge 2];then

???????????? Critical= "TMS Application service is faulty, please deal with it urgently!! "

???????????? echo $Critical | Mutt-s "Service Down" www.magedu.com

???????????? Exit

???????? Fi

???? Done

?

}

?

6. print a number between 1-10 with a space divided:

???????? [[email protected] test-scripts]# seq-s "" 10

1 2 3 4 5 6 7 8 9 10

Note:-S is the specified delimiter

7. Special variables for the shell

???? As shown in the following:

????????

?

?

???????? 7.1 $* and [email protected] Difference examples:

???????????????? $* treats all parameters of the command line as a single string, equivalent to "$1$2$3

???????????? [email protected] treats each parameter of the command line as a separate substring, equivalent to "$" "$" "$". This is the best way to pass parameters to other programs because he retains any whitespace embedded in each parameter.

Actual Demo:

[[Email protected] test-scripts]# set--"I am" Handsome boy #=== passed in three parameters

[[email protected] test-scripts]# echo $# #==è now has three parameters

3

[[email protected] test-scripts]# for i in $*;d o echo $i;d one #循环打印这些参数用 $*, no quotes

I

Am

Handsome

Boy

[[email protected] test-scripts]# for i in [email protected];d o echo $i;d one #没有引号的情况下和 $* results

I

Am

Handsome

Boy

[[email protected] test-scripts]# for i;d o echo $i;d one #--No in variable list, equivalent in "[email protected]"

I am

Handsome

Boy

[[email protected] test-scripts]# for i in "[E-mail protected]";d o echo $i;d One #------in the case of double quotes, the contents of the quotation marks in the argument are output as a parameter. This is the requirement to meet our incoming parameters set– "I am" Handsome boy.

I am

Handsome

Boy

[[email protected] test-scripts]# for I in "$*";d o echo $i;d one #--with double quotes, $* represents a string

I am Handsome Boy

[[email protected] test-scripts]# Shift #---Remove the first argument with shift

[Email protected] test-scripts]# echo $#

2

[[email protected] test-scripts]# for i in "[email protected]";d o echo $i;d one #--again prints only the parameters left behind

Handsome

Boy

?

8. Common operations for variable quantum strings

???? Let me give an example to my friends:

Define the Changsheng variable with the content "I am Chang Sheng"

[Email protected] etc]# changsheng= "I am Chang Sheng"

[[email protected] etc]# echo ${changsheng}

I am Chang Sheng????

    1. Returns the length of a string changsheng a variable string

[Email protected] ~]# echo ${#changsheng}

16

?

2) intercept the Changsheng variable string from the second character onwards, the default takes all of the following characters, the 2nd character is not included.???????? It can also be understood to delete the number of characters in front

[[email protected] ~]# echo ${changsheng:2}

Am Chang Sheng

?

?

3) intercept the Changsheng variable character from the second character and take two characters.

[[email protected] ~]# echo ${changsheng:2:2}

Am

Tip: Similar to cut–c parameters

[Email protected] ~]# echo ${changsheng}|cut-c 1-4

I am

[Email protected] ~]# echo ${changsheng}|cut-c 3-4

Am

?

?

4) Delete the shortest match "I am" substring starting at the beginning of the variable $changsheng

[[email protected] ~]# echo ${changsheng#i am}

Chang Sheng

?

?

5) Delete the longest match "I am Chang substring" starting from the beginning of the variable $changsheng

[Email protected] ~]# echo ${changsheng# #I am Chang}

Sheng

6)???? ${var%word*}: From right to left, delete the first word that appears at the beginning of the character until all the characters in the tail;

???????????? ${var%%word*}: From right to left, delete all characters at the beginning of the last word occurrence until the end of the character;

?

Example in a system script:

[-Z "${columns:-}"] && columns=80

??

[-Z "${consoletype:-}"] && consoletype= "$ (/sbin/consoletype)"

?

?

The following examples illustrate:

(1) ${value:-world}

Returns the world content when the variable is undefined or empty, otherwise the value of the variable is returned

[[email protected] ~]# Result=${test:-unset}

[Email protected] ~]# echo $result

UNSET

[Email protected] ~]# echo $test

[Email protected] ~]#

Conclusion: When the test variable has no content, it returns to the back of the unset. But did not assign a value to result

(2) ${value:=word}

[[email protected] ~]# unset result

[Email protected] ~]# echo $result

?

?

[Email protected] ~]# unset test

[Email protected] ~]# echo $test

?

[[email protected] ~]# Result=${test:=unset}

[Email protected] ~]# echo $result

UNSET

[Email protected] ~]# echo $test

UNSET

?

Hint: When a variable does not exist, it assigns a value to the variable after it

?

?

9. Variable Substitution Table

13. Common file Test operations

?

Integer two-dollar comparison operator

?

?

?

?

?

?

Multi-branch structure

?

?

?

?

?

?

?

?

?

Function:

?

Bash Script Programming Knowledge reserve

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.