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? ...
随机推荐
- T4扩展程序
T4功能强大,不用简直就是浪费青春.vs是没有自带对T4模板编辑的扩展的,写着很累很累(我很low我承认) 这两个驯兽师能帮你驯服它 Devart T4 Editor T4 Toolbox
- BZOJ1226 SDOI2009学校食堂(状压dp)
由于Bi<=7,考虑状压. 如果考虑前i个位置的话,状态里需要压入前7个人后7个人,显然是跑不动的. 那么改成考虑前i个人.于是设f[i][j][k]表示前i个人都已吃完饭,i+1后面7个人的吃 ...
- c语言基本数据类型(short、int、long、char、float、double)
一 C 语言包含的数据类型 short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 在不同的系统上,这些类型占据的字节长度是不同的: 在32 ...
- python selenium2 有关cookie操作实例及如何绕开验证码
1.先看一下cookie是啥 cookie是访问web时服务器记录在用户本地的一系列用户信息(比如用户登录信息),以便对用户进行识别 from selenium import webdriver im ...
- SpringBoot之mongoTemplate的使用
springboot的版本1.5.17.RELEASE. 1.mongo的IP和端口 在resources下的application.properties中加入如下内容 spring.data.mon ...
- Spring点滴六:Spring中定义bean的继承
在基于XML配置元数据中,bean标签可以包含很多配置信息,可以包含构造函数的参数,属性值以及其他一些初始化方法.子bean的定义可以继承父bean定义元数据,子bean定义可以根据需要重写父bean ...
- BZOJ 3230 相似子串 | 后缀数组 二分 ST表
BZOJ 3230 相似子串 题面 题解 首先我们要知道询问的两个子串的位置. 先正常跑一遍后缀数组并求出height数组. 对于每一个后缀suffix(i),考虑以i开头的子串有多少是之前没有出现过 ...
- View的setLayerType() , setDrawingCacheEnabled() 方法用法
一.Android开发:用getDrawingCache方法获取ImageView中的图像需要注意的问题http://www.linuxidc.com/Linux/2011-09/43131.htm ...
- php 中的错误处理机制
php 里有一套错误处理机制,可以使用 set_error_handler 接管 php 错误处理,也可以使用 trigger_error 函数主动抛出一个错误. set_error_handler( ...
- 【转】了解CNN
摘要 过去几年,深度学习在解决诸如视觉识别.语音识别和自然语言处理等很多问题方面都表现出色.在不同类型的神经网络当中,卷积神经网络是得到最深入研究的.早期由于缺乏训练数据和计算能力,要在不产生过拟合的 ...