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. C++模式学习------适配器模式

    适配器模式: 适配器模式属于结构型的设计模式,是将一个类的接口转换成使用方希望的另外一个接口,这样使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器模式有两种: 1.类的适配器:继承不 ...

  2. Qt 删除目录

    删除目标的目录,若该目录下有子目录,一并删除. //判断是否存在子目录 bool judgeDir(QDir dir) { dir.setFilter(QDir::AllEntries | QDir: ...

  3. 整理:python的二维数组操作

    Python中初始化一个5 x 3每项为0的数组,最好方法是: multilist = [[0 for col in range(5)] for row in range(3)] 如果初始化一个二维数 ...

  4. MT【129】常数变易法

    已知数列\(\{x_n\}\)满足\[x_{n+1}=\left(\dfrac 2{n^2}+\dfrac 3n+1\right)x_n+n+1,n\in\mathbf N^*,\]且\(x_1=3\ ...

  5. 【刷题】LOJ 6000 「网络流 24 题」搭配飞行员

    题目描述 飞行大队有若干个来自各地的驾驶员,专门驾驶一种型号的飞机,这种飞机每架有两个驾驶员,需一个正驾驶员和一个副驾驶员.由于种种原因,例如相互配合的问题,有些驾驶员不能在同一架飞机上飞行,问如何搭 ...

  6. 【刷题】洛谷 P3613 睡觉困难综合征

    题目背景 刚立完Flag我就挂了WC和THUWC... 时间限制0.5s,空间限制128MB 因为Claris大佬帮助一周目由乃通过了Deus的题,所以一周目的由乃前往二周目世界找雪辉去了 由于二周目 ...

  7. Tomcat8.0配置JNDI多数据源

    jndi配置 :此种配置需要在Tomcat的server.xml中和context.xml中配置数据源,在项目中引用. 需要在tomcat下加入数据库连接的jar包,相关包(ojdbc14;c3p0数 ...

  8. Mininet 系列实验(二)

    实验内容 分别通过命令行创建.Python脚本编写以及交互式界面创建来熟悉Mininet的基本功能. 参考 Mininet命令延伸实验扩展 实验环境 虚拟机:Oracle VM VirtualBox ...

  9. PyQt5整体介绍

    1 PyQt5整体介绍 PyQt5是基于图形程序框架Qt5的Python语言实现,由一组Python模块构成. PyQt5的官方网站是:www.riverbankcomputing.co.uk. Py ...

  10. Python Opencv 色彩平衡

    在平衡良好的照片中,最亮的颜色应该是白色和最暗的黑色. 因此,我们可以通过缩放每个R,G和B通道的直方图来移除图像中的色偏,使得它们跨越完整的0-255比例 网上只有C的代码 修改为Python代码 ...