Bash script 15-minute Advanced Guide

Source: Internet
Author: User
Tags echo b echo d
The technical skills here were initially from Google's TestingontheToilet (TOTT ). Here is a revised and expanded version. Script security all my bash scripts have the following opening remarks :#! /Bin/bashset-onounsetset-oerrexit this will avoid

The technical skills here were initially from Google's "Testing on the Toilet" (TOTT ). Here is a revised and expanded version.

Script security

All my bash scripts have the following opening remarks:

    #!/bin/bash    set -o nounset    set -o errexit

This avoids two common problems:

  1. Reference undefined variables (default value: "")
  2. Commands that fail to be executed are ignored.

Note that some parameters of some Linux commands can forcibly ignore errors, such as "mkdir-p" and "rm-f ".

Note that in errexit mode, although errors can be effectively captured, all failed commands cannot be captured. in some cases, some failed commands cannot be detected. (For more details, refer to this post .)

Script functions

In bash, you can define functions that can be used at will just like other commands. they make your scripts more readable:

    ExtractBashComments() {        egrep "^#"    }     cat myscript.sh | ExtractBashComments | wc     comments=$(ExtractBashComments < myscript.sh)

There are also some examples:

    SumLines() {  # iterating over stdin - similar to awk              local sum=0        local line=””        while read line ; do            sum=$((${sum} + ${line}))        done        echo ${sum}    }     SumLines < data_one_number_per_line.txt     log() {  # classic logger       local prefix="[$(date +%Y/%m/%d\ %H:%M:%S)]: "       echo "${prefix} $@" >&2    }     log "INFO" "a message"

Try to move your bash code into the function, and only place the global variables, constants, and statements called for "main" in the outermost layer.

Variable annotation

Bash can make limited annotations to variables. The two most important annotations are:

  1. Local (internal variable of function)
  2. Readonly (read-only variable)
    # a useful idiom: DEFAULT_VAL can be overwritten    #       with an environment variable of the same name    readonly DEFAULT_VAL=${DEFAULT_VAL:-7}     myfunc() {       # initialize a local variable with the global default       local some_var=${DEFAULT_VAL}       ...    }

In this way, you can declare a variable that was previously not a read-only variable as a read-only variable:

x=5x=6readonly xx=7   # failure

Try to annotate all variables in your bash script using local or readonly.

Use $ () instead of single quotes (')

The unquoted quotation marks are ugly. in some fonts, they are similar to the regular quotation marks. $ () Can be embedded and used to avoid the trouble of escape characters.

# both commands below print out: A-B-C-Decho "A-`echo B-\`echo C-\\\`echo D\\\`\``"echo "A-$(echo B-$(echo C-$(echo D)))"
Use [[] (double brackets) to replace []

[[] Can be used to avoid problems such as abnormal file extensions, Improve syntax, and add many new features:

Operator Function description
| Logic or (used only in double brackets)
&& Logical and (used only in double brackets)
< String comparison (transfer is not required in double brackets)
-Lt Number comparison
= Equal strings
= String comparison in Globbing mode (used only in double brackets, refer to the following)
= ~ Use regular expressions to compare strings (used only in double brackets)
-N Non-null string
-Z Null string
-Eq Equal number
-Ne Numbers

Brackets:

[ "${name}" \> "a" -o ${name} \< "m" ]

Double brackets

[[ "${name}" > "a" && "${name}" < "m"  ]]
Regular expression/Globbing

The advantages of using double brackets are shown in the following examples:

T = "abc123" [["$ t" = abc *] # true (globbing comparison) [["$ t" = "abc *"] # false (literal comparison) [["$ t" = ~ [Abc] + [123] +] # true (regular expression comparison) [["$ t" = ~ "Abc *"] # false (literal comparison)

Note that regular expressions and globbing expressions cannot be enclosed in quotation marks from bash 3.2. If your expression contains spaces, you can store it in a variable:

r="a b+"[[ "a bbb" =~ $r ]]        # true

You can also use the case statement to compare strings in the Globbing mode:

case $t inabc*)   ;;esac
String operation

Bash has a variety of string operations, many of which are not desirable.

Basic User

F = "path1/path2/file. ext" len = "$ {# f}" # = 20 (string length) # slice operation: $ {:
  
   
} Or $ {
   :
    
     
:
     
      
} Slice1 = "$ {f: 6}" # = "path2/file. ext "slice2 =" $ {f: 6: 5} "# =" path2 "slice3 =" $ {f:-8} "# =" file. ext "(note:"-"contains spaces) pos = 6 len = 5 slice4 =" $ {f :$ {pos }: $ {len} "# =" path2"
     
    
  

Replace (use globbing)

F = "path1/path2/file. ext" single_subst = "$ {f/path? /X} "# =" x/path2/file. ext "global_subst =" $ {f // path? /X} "# =" x/file. ext "# string splitting readonly DIR_SEP ="/"array = ($ {f // $ {DIR_SEP}/}) second_dir =" $ {arrray [1]} "# = path2

Delete the header or tail (use globbing)

F = "path1/path2/file. ext "# Delete the string header extension =" $ {f #*.} "# =" ext "# Delete the string header filename =" $ {f ##*
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.