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.

10 Useeful Tips for Writing Effective Bash Scripts in Linux的更多相关文章

  1. 10 Tips for Writing Better Code (阅读理解)

    出发点 http://www.tuicool.com/articles/A7VrE33 阅读中文版本<编写质优代码的十个技巧>,对于我编码十年的经验,也有相同感受, 太多的坑趟过,太多的经 ...

  2. 写出完美论文的十个技巧10 Tips for Writing the Perfect Paper

    10 Tips for Writing the Perfect Paper Like a gourmet meal or an old master painting, the perfect col ...

  3. Tips for writing a paper

    Tips for writing a paper 1. Tips for Paper Writing 2.• Before you write a paper • When you are writi ...

  4. Ten Tips for Writing CS Papers, Part 2

    Ten Tips for Writing CS Papers, Part 2 This continues the first part on tips to write computer scien ...

  5. Ten Tips for Writing CS Papers, Part 1

    Ten Tips for Writing CS Papers, Part 1 As a non-native English speaker I can relate to the challenge ...

  6. 使用ssh client与bash scripts轻松管理多台主机

    当我们需要控制一个局域网中的很多台服务器时,一个简单的全局操作可能会被放大地异常繁琐,这时我们就会需要新的工具来快速完成这种工作. 我们将使用ssh客户端提供的一些工具来快速完成这一开发工作,我们的开 ...

  7. centos 7安装mysql报错-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory

    [root@localhost mysql]# ./scripts/mysql_install_db  --user=mysql -bash: ./scripts/mysql_install_db: ...

  8. 10 quick tips for Redis

    Redis is hot in the tech community right now. It's come a long way from being a small personal proje ...

  9. 17 Tips For Writing An Excellent Email Subject Line

    Out of the billions of emails that are sent every day, how can you make sure that yours stands out? ...

随机推荐

  1. Java中线程安全的集合

    如果多线程并发的访问与一个数据结构,那么很容易破坏一个数据结构. 例如,一个线程可能要向一个散列表中插入一条数据的过程中,被剥夺了控制权.如果另外一个线程也开始遍历同一个链表,很可能造成混乱,抛出异常 ...

  2. java中的==操作符和equals函数

    基本规则 “==”操作符的使用需要分成两种情况 判值类型相等 这一点很好理解,两个值类型代表的数值相等,则“==”表达式返回true “==”可以用与不同值类型的比较,语言会自动进行类型转换 判引用类 ...

  3. js时间戳转换日期格式和日期计算

    一.时间戳转换日期 function formatDate(datetime) { // 获取年月日时分秒值 slice(-2)过滤掉大于10日期前面的0 var year = datetime.ge ...

  4. 百度地图经纬度批量查找功能XGeocoding使用手册

    <XGeocoding使用手册> 1.下载XGeocoding V2 http://www.gpsspg.com/xgeocoding/download/ 2.解压XGeocoding_v ...

  5. 设计模式之单例模式实现(C++)

    #ifndef SINGLETON_H #define SINGLETON_H #include <cassert> #include <memory> #include &l ...

  6. opencv图片右转函数

    因为需要将函数进行右转,发现opencv自带 的过于麻烦.自己写了个右转的.可以根据这个想法写出任何方向的 //函数功能,右转图片 IplImage* convertImage(IplImage* i ...

  7. img、列表和table标签

    一.img图片 <body> <a href="https://www.fmtxt.com"> <img src="images/1.jpg ...

  8. python基础之函数进阶之函数作为返回值/装饰器

    因为装饰器需要用到返回函数的知识,所以在这里将返回函数和装饰器合并讲解. 什么是返回函数? 我们知道,一个函数中return可以返回一个或者多个值,但其实,return不仅可以返回值,还可以返回函数. ...

  9. mysql 同步数据到 ElasticSearch 的方案

    MySQL Binlog 要通过 MySQL binlog 将 MySQL 的数据同步给 ES, 我们只能使用 row 模式的 binlog.如果使用 statement 或者 mixed forma ...

  10. java基础-BigInteger类常用方法介绍

    java基础-BigInteger类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigInteger类概述 Java中long型为最大整数类型,对于超过long ...