10 Useeful Tips for Writing Effective Bash Scripts in Linux

1.Always Use Comments in Scripts
2.Make a Scripts exit When Fails
    Sometimes bash may continue to execute a script even when a certain command fails.thus affecting the rest of the script (may eventually result in logical errors).Use the Command below to exit a script when a command fails.
    #let script exit if a command fails(remember:this command should use at the top of the scripts)
    set -o errexit
    OR
    set -e
3.Make a Scripts exit When Bash uses Undeclared Variable
    Bash may also try to use an undeclared script which could cause a logical error.Therefore use the following line to instruct bash to exit a script when it attemps to use an undeclared variable.
    #let script exit if an unused variable is used
    set -o nounset
    OR
    set -u
4.Use double quotes to Reference variables
    Use double quotes while referencing (using a value of a variable) helps to prevent word splitting (regarding whitespace) and unnecessary globbing匹配(recognizing and expanding wildcards)
    eg:
        #!/bin/bash
        #let a script exit if a command fails
        set -o errexit
        #let a script exit if an unsed variable is used
        set -o nounset
        echo "Names without double quotes"
        echo
        names="Michael Jordan"
        for name in $names;do
        echo "$name"
        done
        
        echo
        echo "Names with double quotes"
        echo
        for name in "$names"; do
        echo "$name"
        done
        exit 0
    (
        1.echo 单独执行表示换行
        2.for 变量 in 列表
          do
            语句
          done
    )

5.Use Functions in Scripts
    Expect for very small scripts (with a few lines of code ),always remember to use functions to modularize your code and make scripts more readable and reusable)
    The sysntax for writing functions is as follows:
    function chech_root(){
    command1;
    command2;
    }
    OR
    chech_root(){
    command1;
    command2;
    }
6.Use = instead of == for String Comparisons
    Note that == is a synonym同义词 for = ,therefore use only a single = for string comparisions,for instance
    value1="baidu.com"
    value2="dubai.com"
    if["$value1"="$value2"]
7.Use $(command) instead of legacy `command` for substitution(替换)
    user=`echo "$UID"`
    user=$(echo "$UID")
8.Use Read-only to Declare Static Variables
    A static variable doesn't change;its value can not be altered once it's defined in a script
    eg:readonly passwd_file="/etc/passwd"
        readonly group_file="/etc/group"
9.Use Uppercase Names for ENVIRONMENT Variables and Lowercase for Custom Variables
    All bash environment variables are named with uppercase letters ,therefore use lowercase letters to name your custom variables to avoid variable name conflicts
10.Always Perform Debugging for Long Scripts
    Methods of Enabling Shell Script Debugging Mode
    Below are the primary shell script debugging options:
    -v (short for verbose冗长) – tells the shell to show all lines in a script while they are read, it activates verbose mode.
    -n (short for noexec or no ecxecution) – instructs the shell read all the commands, however doesn’t execute them. This options activates syntax checking mode.
    -x (short for xtrace or execution trace) – tells the shell to display all commands and their arguments on the terminal while they are executed. This option enables shell tracing mode.

    

原文地址:https://www.cnblogs.com/jycjy/p/7000301.html