Introduction
This is a simple bash script creation automation script. The script “nbs” is an acronym for “new bash script”, which allows for easy automation while creating other bash scripts with a command: nbs newscript.sh
.
Why?
In my work flow, the creation of bash script for my system usually contains a few steps, mainly:
- Checking if script exists
- Edit the existing script OR
- Create a new script
- Typing out long commands to make a new script
vim $HOME/.local/bin/script
- Adding shebang
- #!/bin/bash
- Making the script executable
chmod +x $HOME/.local/bin/script
- Adding a short description of the code
A bash script would be able to automate these steps by using a single command.
Dependencies
These dependencies should already be installed in your system.
- Unix Shell (Bash/ZSH)
- Editor (Vim/Nano/etc.)
Code
Introduction
This scripts assumes that you have a linux file structure. There will be 2 files: bash_template and nbs, both stored in $HOME/.local/share/template/
and $HOME/.local/bin/ respectively
.
Template
Create the folder, this is where we will store the templates for new scripts
|
|
Create and edit a new template
|
|
|
|
This is my template for creating new bash scripts. It contains the shebang, author details and a comment section to allow for a short description of the script.
G
+ i
Script
Create and edit the script
|
|
You can copy the template to the new script
|
|
The first line of this script will check for the name of the newly created script as an argument.
|
|
The next line will allow for editing the file, if the file exists
|
|
Read more about conditional tags here
The last line will create a new file - Copy the template, make the file executable and edit the new file
|
|
Summary
In 3 lines, this script is able to do conditional checking, copying of template and editing the script. This shows the ability of bash scripts. The full code will be listed below, do check it out if you have problems.
With this script, you will be able to create a new bash script from your terminal
|
|
Full Code
nbs
|
|
bash_template
|
|
Fixing Errors
$PATH
Ensure that $HOME/.local/bin/
is in your $PATH. If unsure, add the following lines to your ~/.bashrc
or ~/.zshrc
|
|
This recursively adds $HOME/.local/bin
and containing folders to $PATH.
Permissions
Ensure that nbs is executable
|
|
Script Improvements
There are certain improvements that may allow the script to be modular. For example, in the script nbs, there could be a configuration section which allows users to configure the script path, template path or default editor.
This is how the script could look like after improvements
|
|
You could make further modifications for your use cases or use the same format to automate the creations of scripts for other languages.