Git Product home page Git Product logo

shell-script-template's Introduction

Shell Script Template

Bash

This is the baseline Bash shell script template I use for new projects, from the simple to the complex.

A huge hat tip to the original foundation I used for this on betterdev.blog (raw source). Another blog resource I referenced and learned from was on betterprogramming.pub.

This has been tested and used almost exclusively on macOS, but I don't think there is anything specific to macOS built into the script, or at least not to the detriment of any other platforms.

How To Use

Copy the shell-script-template.sh file into your new script:

cp shell-script-template.sh my-new-script.sh

Customize the script for your solution.

Hint: Search for "TODO", the areas that require focus are tagged with TODOs.

The highest priority changes are to modify the usage function to print out a summary about the script and update/remove the options for your script, update the argument parsing that corresponds to the usage information, then implement the body of the script.

There are some "nice to have" changes like adding parameter validation, additional utility functions, etc.

Misc. Info and Resources

Parsing Parameters/Arguments

You will notice that getopt is not used, this is because of an incompatibility with macOS.

Colorized Output

Colorized output uses 3 and 4-bit colors (more info on Wikipedia) and has only really been tested on very dark backgrounds. Where multiple variants of a color exist, I typically used the bright variant. There is also some great discussion on the topic on StackExchange.

ShellCheck LINTing

I highly recommend the ShellCheck LINTing tool, it provides a lot of really useful feedback regarding problematic or error-prone aspects of a shell script.

Testing

Unit testing for shell scripts is a wide-ranging topic with no shortage of available libraries. I have only just begun this research, but so far this summary on Medium from wemake-services seems to have a nice concise outline of pros/cons of some common libraries. This dodie/testing-in-bash repo also contains a useful overview of feature support for popular libraries.

I am inclined to use bats-core, but I suspect there are several good choices.

shell-script-template's People

Contributors

mikeoertli avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.