A Bash scripting template incorporating best practices & several useful functions.
I write Bash scripts frequently and realised that I often copied a recent script whenever I started writing a new one. This provided me with a basic scaffold to work on and several useful helper functions I'd otherwise likely end up duplicating.
Rather than continually copying old scripts and flensing the irrelevant code, I'm publishing a more formalised template to ease the process for my own usage and anyone else who may find it helpful. Suggestions for improvements are most welcome.
File | Description |
---|---|
template.sh | A fully self-contained script which combines source.sh & script.sh |
source.sh | Designed for sourcing into scripts; contains only those functions unlikely to need modification |
script.sh | Sample script which sources in source.sh and contains those functions likely to be modified |
build.sh | Generates template.sh by combining source.sh & template.sh (just a helper script) |
Being a Bash script you're free to slice-and-dice the source as you see fit.
The following steps outline what's typically involved to help you get started:
template.sh
(fully self-contained)script.sh
with source.sh
(source in most functions)template.sh
or script.sh
for editingscript_usage()
function with additional usage guidanceparse_params()
function with additional script parametersmain()
function to call your additional functionshostname
parameterThe following contrived example demonstrates how to add a parameter to display the system's hostname.
Update the script_usage()
function by inserting the following before the EOF
:
--hostname Display the system's hostname
Update the parse_params()
function by inserting the following before the default case statement (*)
):
--hostname)
hostname=true
;;
Update the main()
function by inserting the following after the existing initialisation statements:
if [[ -n ${hostname-} ]]; then
pretty_print "Hostname is: $(hostname)"
fi
The Bash scripting community is an opinionated one. This is not a bad thing, but it does mean that some decisions made in this template aren't going to be agreed upon by everyone. A few of the most notable ones are highlighted here with an explanation of the rationale.
Conventional wisdom has for a long time held that at the top of every Bash script should be set -e
(or the equivalent set -o errexit
). This modifies the behaviour of Bash to exit immediately when it encounters a non-zero exit code from an executed command if it meets certain criteria. This would seem like an obviously useful behaviour in many cases, however, controversy arises both from the complexity of the grammar which determines if a command is eligible for this behaviour and the fact that there are many circumstances where a non-zero exit code is expected and should not result in termination of the script. An excellent overview of the argument against this option can be found in BashFAQ/105.
My personal view is that the benefits of errexit
outweigh its disadvantages. More importantly, a script which is compatible with this option will work just as well if it is disabled, however, the inverse is not true. By being compatible with errexit
those who find it useful can use this template without modification while those opposed can simply disable it without issue.
By enabling set -u
(or the equivalent set -o nounset
) the script will exit if an attempt is made to expand an unset variable. This can be useful both for detecting typos as well as potentially premature usage of variables which were expected to have been set earlier. The controvery here arises in that many Bash scripting coding idioms rely on referencing unset variables, which in the absence of this option are perfectly valid. Further discussion on this option can be found in BashFAQ/112.
This option is enabled for the same reasons as described above for errexit
.
All content is licensed under the terms of The MIT License.
Safe Bash Script Template This is a note on this blog regarding a basic template to create secure and functional bash scripts. The author is Maciej with github and linkedin][(https://www.linkedin.com/
文档 electron文档 electron-vue-template文档 实现主进程和渲染进程通信 Electron 提供了两个模块:ipcMain 和 ipcRenderer //frontend\src\main\services\ipcMain.js import { ipcMain, dialog, BrowserWindow } from 'electron' export defau
内容持续更新可查看 Notion 链接 推荐开发工具 VSCode + Volar 集成说明 技术栈:vue 3.x、vite 2.x、pinia 2.x、Vue Router 4.x、TypeScript 等 代码风格检查约束:ESLint + Prettier、husky + lint-staged 环境相关配置 浏览器兼容性 … 搭建步骤 创建项目 yarn create vite 选择 v
每个人或多或少总会碰到要使用并且自己完成编写一个最基础的Bash脚本的情况。真实情况是,没有人会说“哇哦,我喜欢写这些脚本”。所以这也是为什么很少有人在写的时候专注在这些脚本上。 我本身也不是一个Bash脚本专家,但是我会在本文中跟你展示一个最基础最简单的安全脚本模板,会让你写的Bash脚本更加安全实用,你掌握了之后肯定会受益匪浅。 为什么要写Bash脚本 其实关于Bash脚本最好的解释如下: T
Client # vmstat 1 2, 一秒间隔,取两次,因为发现第一次不准确 command[cacti_vmstat]=/usr/bin/vmstat 1 2 Server Data Input Method /bin/bash <path_cacti>/scripts/bigbig_vmstat_cpu.sh <server_addr> #!/bin/bash # bigbig_vmsta
引入js文件 <script src="../../lib/template-web.js"></script> 在body中定义容器 <div id="content"></div> 创建template <script id="test" type="text/html"> {{if isAdmin}} <ul> {{each Un
Mac OS X: 实用脚本程序(bash scripts)系列 更新历史 : 2009-06-23: 添加关于祛除打印机共享的选项,并为了突出代码而改变字体和颜色。 2009-06-22: 添加有关用户组的部分(Group ID和Group) 添加说明的第4项. 变更ARD命令和说明中的MYADMIN. 说明
Aircrack-ng BASH Script 是 Linux 下用来测试无线网络安全的脚本。 要使用该脚本必须先安装:Aircrack-ng, Kismet, Macchanger, and Xterm.
问题内容: 我正在尝试使用命令记录bash会话。 该命令是从bash脚本开始执行的,但是一旦执行,bash脚本就会终止。 我尝试使用各种组合来始终以相同的结果调用命令(调用该命令后立即终止bash脚本)。我得到的输出如下: 我也尝试过最后用a来调用命令,但是再次失败了。 谁能告诉我如何从bash脚本调用命令? 谢谢 问题答案: 您的Shell脚本没有终止。它仍在运行。您会收到提示,因为正在生成新的
<script> defer 和 async 有什么区别? 相同点: <script> 标签必须有 src 属性,不能是内联脚本 加载是异步的 脚本中不能调用 document.write() 不同点: defer 在 HTML 4 中被定义,async 在 HTML 5 中定义 defer 使脚本在 HTML 解析完且触发 DOMContentLoaded 之前按照声明顺序执行,async 则是
我们把项目查看模式切换成Android,所有的文件会通过类型进行归类,这个并不是实际在电脑中的文件结构哦,如果想看实际的物理结构请切换到Project. 切换成Android可以查看所有的Gradle Script: 每个文件后面都有一个灰色字体描述: 1.build.gradle: Project构建文件 2.build.gradle: Module构建文件 3.gradle.propertie
SCRIPT FLUSH 清除所有 Lua 脚本缓存。 关于使用 Redis 对 Lua 脚本进行求值的更多信息,请参见 EVAL 命令。 可用版本: >= 2.6.0 复杂度: O(N) , N 为缓存中脚本的数量。 返回值: 总是返回 OK redis> SCRIPT FLUSH OK
SCRIPT KILL 杀死当前正在运行的 Lua 脚本,当且仅当这个脚本没有执行过任何写操作时,这个命令才生效。 这个命令主要用于终止运行时间过长的脚本,比如一个因为 BUG 而发生无限 loop 的脚本,诸如此类。 SCRIPT KILL 执行之后,当前正在运行的脚本会被杀死,执行这个脚本的客户端会从 EVAL 命令的阻塞当中退出,并收到一个错误作为返回值。 另一方面,假如当前正在运行的脚本已