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? ...
随机推荐
- delphi ERP框架
之前做c/s架构,接了有家装饰的一个ERP项目,做了一个ERP框架,现在转后端开发了,这些东西还是蛮怀念的,就开源出来吧,有需要的同学可以参考. https://github.com/qianlnk/ ...
- SSL证书部署
SSL证书部署指南 https://www.trustauth.cn/ssl-guide
- UVAlive4080_Warfare And Logistics
给一个无向图,求出两个值,所有点到所有其他点的最短距离和,任意删除一条边后的这个值. 数据规模是100点1000边. 白书例题,不多说了直接对于每个点求出最短路树,对于每条边,如果它不是最短路树上的边 ...
- Windows平台下在服务中添加MySQL
widows下查看服务 1.桌面计算机-->右键-->管理-->计算机管理(本地)--->服务和应用程序-->服务 2.运行 中输入 services.msc 在服务中添 ...
- Git常用的几个命令
标签(空格分隔): Git 在本地文件系统中新建目录,放置你的工程: mk dir parkk cd parkk //进入该目录 git init //初始化自己的仓库,默认名称为master 在仓库 ...
- Round 1 Over
终于把题目清单上的 \(dp\) 写完了\(hhh\)
- 【转】一口气读懂NB-IoT
在过去的一年多,NB-IoT真的可以说是大红大紫.在通信圈里,除了说5G,就是说物联网.如果说物联网,八成就是在说NB-IoT. 在目前5G还没来的情况下,NB-IoT基本上是独领风骚.风光无限. 各 ...
- 【bzoj1483】 HNOI2009—梦幻布丁
http://www.lydsy.com/JudgeOnline/problem.php?id=1483 (题目链接) 题意 $n$个布丁摆成一行,进行$m$次操作.每次将某个颜色的布丁全部变成另一种 ...
- NTT+多项式求逆
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- [POI2012]STU-Well
题意翻译 给定一个非负整数序列A,每次操作可以选择一个数然后减掉1,要求进行不超过m次操作使得存在一个Ak=0且max(∣xi−xi−1∣)最小,输出这个最小值以及此时最小的k (1≤n≤1 000 ...