Home

Awesome

Bash CI Shellcheck CI version activity license repo size

BASH BOILERPLATE

UPDATE: This repo is no longer under development!! please check github.com/pforret/bashew for a more modern version of this project

It's like a mini console framework for bash shell scripting.

Just use one of 4 methods to generate a new script, that has all the functionality to

  1. one self-contained file, no external dependencies

  2. parse options and parameters

  3. generate clean usage

  4. run in silent/quiet or verbose mode

  5. create and clean up temporary folder/files

  6. better error reporting

  7. Bash CI (Github Actions)

  8. self-initialisation for new scripts (script.sh init)

    flag|h|help|show usage
    flag|q|quiet|no output
    flag|v|verbose|output more
    flag|f|force|do not ask for confirmation
    option|l|logd|folder for log files |log
    option|t|tmpd|folder for temp files|.tmp
    param|1|action|action to perform: LIST/TEST/...
    param|1|output|output file
    # there can only be 1 param|n and it should be the last
    param|n|inputs|input files
    

becomes

USAGE

  Program: script.sh by @email
  Version: @version (L:591-MD:6523f7)
  Updated: Jul 31 20:07:24 2020
  Usage: script.sh [-h] [-q] [-v] [-f] [-l <logd>] [-t <tmpd>] <action> <output> <inputs …>
  Flags, options and parameters:
      -h|--help      : [flag] show usage [default: off]
      -q|--quiet     : [flag] no output [default: off]
      -v|--verbose   : [flag] output more [default: off]
      -f|--force     : [flag] do not ask for confirmation (always yes) [default: off]
      -l|--logd <val>: [optn] folder for log files   [default: log]
      -t|--tmpd <val>: [optn] folder for temp files  [default: .tmp]
      <action>  : [parameter] action to perform: init/list/test/...
      <output>  : [parameter] output file
      <inputs>  : [parameters] input files (1 or more)
  

SCRIPT AUTHORING TIPS

  * use out to show any kind of output, except when option --quiet is specified
    out "User is [$USER]"
  * use progress to show one line of progress that will be overwritten by the next output
    progress "Now generating file $nb of $total ..."
  * use is_empty and is_not_empty to test for variables
    if is_empty "$email" ; then ; echo "Need Email!" ; fi
  * use die to show error message and exit program
    if [[ ! -f $output ]] ; then ; die "could not create output" ; fi
  * use alert to show alert message but continue
    if [[ ! -f $output ]] ; then ; alert "could not create output" ; fi
  * use success to show success message but continue
    if [[ -f $output ]] ; then ; success "output was created!" ; fi
  * use announce to show the start of a task
    announce "now generating the reports"
  * use log to show information that will only be visible when -v is specified
    log "input file: [$inputname] - [$inputsize] MB"
  * use escape to extra escape '/' paths in regex
    sed 's/$(escape $path)//g'
  * use lcase and ucase to convert to upper/lower case
    param=$(lcase $param)
  * use confirm for interactive confirmation before doing something
    if ! confirm "Delete file"; then ; echo "skip deletion" ; fi
  * use ask for interactive setting of variables
    ask NAME "What is your name" "Peter"
  * use on_mac/on_linux/'on_32bit'/'on_64bit' to only run things on certain platforms
    on_mac && log "Running on MacOS"
  * use folder_prep to create a folder if needed and otherwise clean up old files
    folder_prep "$logd" 7 # delete all files olders than 7 days

VERSION HISTORY

CREATE NEW BASH SCRIPT

Option 1: clone this repo

git clone https://github.com/pforret/bash-boilerplate.git
cp bash-boilerplate/script.sh my-new-script.sh

Option 2: create new Github repo from template

go to https://github.com/pforret/bash-boilerplate - press "Use this template"

Option 3: download the script directly

wget https://raw.githubusercontent.com/pforret/bash-boilerplate/master/script.sh
mv script.sh my-new-script.sh

Option 4: customize parameters and copy/paste

go to toolstud.io/data/bash.php

EXAMPLES

These scripts were made with some version of bash-boilerplate

ACKNOWLEDGEMENTS

I learned a lot of tips from these sources: