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.
10 Useeful Tips for Writing Effective Bash Scripts in Linux的更多相关文章
- 10 Tips for Writing Better Code (阅读理解)
		出发点 http://www.tuicool.com/articles/A7VrE33 阅读中文版本<编写质优代码的十个技巧>,对于我编码十年的经验,也有相同感受, 太多的坑趟过,太多的经 ... 
- 写出完美论文的十个技巧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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
- 使用ssh client与bash scripts轻松管理多台主机
		当我们需要控制一个局域网中的很多台服务器时,一个简单的全局操作可能会被放大地异常繁琐,这时我们就会需要新的工具来快速完成这种工作. 我们将使用ssh客户端提供的一些工具来快速完成这一开发工作,我们的开 ... 
- 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: ... 
- 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 ... 
- 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? ... 
随机推荐
- 《编写高质量代码改善JavaScript程序的188个建议》读书笔记
			逗号运算符比较怪异.如 var a =(1,2,3,4);alert(a);// 4 var a = 1,2,3,4;//报错 注意a++和++a的差别,变量在参与运算中不断地变化.v ... 
- MachineLearning Exercise 7 : K-means Clustering and Principle Component Analysis
			findClosestCentroids.m m = size(X,); :m [value index] = min(sum((repmat(X(i,:),K,)-centroids).^,)); ... 
- 【uoj#209】[UER #6]票数统计  组合数+乱搞
			题目描述 一个长度为 $n$ 的序列,每个位置为 $0$ 或 $1$ 两种.现在给出 $m$ 个限制条件,第 $i$ 个限制条件给出 $x_i$ .$y_i$ ,要求至少满足以下两个条件之一: 序列的 ... 
- P2461 [SDOI2008]递归数列
			题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj 和 cj ... 
- hihocoder1639 图书馆 [数学]
			已知数组a[]及其和sum, 求sum! / (a1!a2!...an!) 的个位数的值. 求某数的逆元表写成了求某数阶乘的逆元表,故一直没找到错误. P 是质数的幂B 表示质数,P 表示模数,cal ... 
- phpredis pipeline
			通过pipeline方式将client端命令一起发出,redis server会处理完多条命令后,将结果一起打包返回client,从而节省大量的网络延迟开销. 
- SDOI 2019 Round1 游记
			\(SDOI~2019 ~ Round1\) 游记 \(Day ~0\) 报道.骑车子去的,好热.到了之后看到好几个同校神仙,还从那里莫名其妙的等了一会,然后交了钱签了名就拿挂牌走人了.现在居然还有受 ... 
- 2017 Multi-University Training Contest - 3
			HDU 6058 #pragma comment(linker, "/STACK:102400000,102400000") #include <bits/stdc++.h& ... 
- Consul 服务发现与配置
			Consule 是什么 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul 的客户端可用提供一个服务,比如 api 或 ... 
- echarts地图扩展___自定义的svg图
			echarts的自定义地图 标签引入js文件 <script type="text/javascript" src="echarts/require.js" ... 
