All Projects → ralish → Bash Script Template

ralish / Bash Script Template

Licence: mit
A best practices Bash script template with several useful functions

Programming Languages

shell
77523 projects
bash
514 projects

Projects that are alternatives of or similar to Bash Script Template

Xorstr
heavily vectorized c++17 compile time string encryption.
Stars: ✭ 435 (-19.89%)
Mutual labels:  template
Template Python
A template for new Python libraries.
Stars: ✭ 479 (-11.79%)
Mutual labels:  template
Jekflix Template
A Jekyll theme inspired by Netflix. 🎬
Stars: ✭ 504 (-7.18%)
Mutual labels:  template
Nestjs Bff
A full-stack TypeScript solution, and starter project. Includes an API, CLI, and example client webapp. Features include production grade logging, authorization, authentication, MongoDB migrations, and end-to-end testing.
Stars: ✭ 450 (-17.13%)
Mutual labels:  template
Fasttemplate
Simple and fast template engine for Go
Stars: ✭ 470 (-13.44%)
Mutual labels:  template
Sing App Vue Dashboard
Vue.js admin dashboard template built with Bootstrap 4.5
Stars: ✭ 482 (-11.23%)
Mutual labels:  template
React Native Template Rocketseat Basic
Template básica para aplicações React Native com a estrutura utilizada na Rocketseat 🚀
Stars: ✭ 431 (-20.63%)
Mutual labels:  template
Yaac Another Awesome Cv
YAAC: Another Awesome CV is a template using Font Awesome and Adobe Source Font.
Stars: ✭ 516 (-4.97%)
Mutual labels:  template
Evie
A production-ready theme for your projects with a minimal style guide https://evie.undraw.co
Stars: ✭ 481 (-11.42%)
Mutual labels:  template
Angularwebpackvisualstudio
Template for ASP.NET Core, Angular with Webpack and Visual Studio
Stars: ✭ 497 (-8.47%)
Mutual labels:  template
Uikit Ecommerce Template
E-commerce template built with UIKIt
Stars: ✭ 453 (-16.57%)
Mutual labels:  template
Dedicated valheim server script
Valheim Menu system for managing or installing your Valheim Dedicated Server. Multi-Language. Supports Vanilla or Mod modes. Built on Ubuntu 20.04.
Stars: ✭ 455 (-16.21%)
Mutual labels:  bash-script
Orgkit
Provision a brand-new company with proper defaults in Windows, Offic365, and Azure
Stars: ✭ 490 (-9.76%)
Mutual labels:  template
Landing Page
Tailwind CSS Starter Template - Landing Page
Stars: ✭ 444 (-18.23%)
Mutual labels:  template
Hartija Css Print Framework
Universal CSS for web printing
Stars: ✭ 509 (-6.26%)
Mutual labels:  template
Asp Net Core Vue Starter
ASP.NET Core + Vue.js starter project
Stars: ✭ 436 (-19.71%)
Mutual labels:  template
Vue Quasar Admin Example
Example of quasar admin
Stars: ✭ 481 (-11.42%)
Mutual labels:  template
Sinatra Template
A base Sinatra application template with DataMapper, and RSpec. Just fork and build.
Stars: ✭ 534 (-1.66%)
Mutual labels:  template
Kalitorify
Transparent proxy through Tor for Kali Linux OS
Stars: ✭ 513 (-5.52%)
Mutual labels:  bash-script
Input Mask Ios
User input masking library repo.
Stars: ✭ 494 (-9.02%)
Mutual labels:  template

bash-script-template

license

A Bash scripting template incorporating best practices & several useful functions.

Motivation

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.

Files

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)

Usage

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:

  1. Choose between using either:
    1. template.sh (fully self-contained)
    2. script.sh with source.sh (source in most functions)
  2. Depending on your choice open template.sh or script.sh for editing
  3. Update the script_usage() function with additional usage guidance
  4. Update the parse_params() function with additional script parameters
  5. Add additional functions to implement the desired functionality
  6. Update the main() function to call your additional functions

Adding a hostname parameter

The 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

Controversies

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.

errexit (set -e)

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.

nounset (set -u)

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.

License

All content is licensed under the terms of The MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].