Skip to content

Instantly share code, notes, and snippets.

@rsperl
Last active February 2, 2023 02:46
Show Gist options
  • Save rsperl/d578646cccd0c951ef568fa12f9e3b41 to your computer and use it in GitHub Desktop.
Save rsperl/d578646cccd0c951ef568fa12f9e3b41 to your computer and use it in GitHub Desktop.

Revisions

  1. rsperl revised this gist Jul 25, 2022. No changes.
  2. rsperl revised this gist Aug 4, 2021. No changes.
  3. rsperl revised this gist Aug 4, 2021. No changes.
  4. Richard Sugg revised this gist May 24, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,2 +0,0 @@
    .gitignore
    .gistinfo.json
  5. Richard Sugg revised this gist May 23, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    .gitignore
    .gistinfo.json
  6. Richard Sugg revised this gist May 23, 2018. 1 changed file with 0 additions and 0 deletions.
    Empty file added .gitignore
    Empty file.
  7. rsperl revised this gist Sep 16, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bash_template.sh
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,8 @@ IFS=$'\n\t'

    action=${1:-"default value"}

    tmpdir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX")

    # write to stderr
    err() {
    echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
  8. rsperl revised this gist Jul 30, 2017. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion bash_template.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,15 @@
    #!/bin/bash

    # https://dev.to/thiht/shell-scripts-matter
    # see also
    # - Google's Shell Style Guide: https://google.github.io/styleguide/shell.xml
    # - ShellCheck: https://github.com/koalaman/shellcheck

    # src:
    # - http://hackaday.com/2017/07/21/linux-fu-better-bash-scripting
    # - https://dev.to/thiht/shell-scripts-matter

    # exit if any line fails (you can do command_fails || true, and the script continues)
    set -o errexit

    set -euo pipefail
    # -e exit if any command returns non-zero status
    @@ -11,6 +20,11 @@ IFS=$'\n\t'

    action=${1:-"default value"}

    # write to stderr
    err() {
    echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
    }

    readonly LOG_FILE="$(basename "$0").log"
    info() { echo "[INFO] $@" | tee -a "$LOG_FILE" >&2 ; }
    warning() { echo "[WARNING] $@" | tee -a "$LOG_FILE" >&2 ; }
  9. rsperl created this gist May 31, 2017.
    29 changes: 29 additions & 0 deletions bash_template.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/bin/bash

    # https://dev.to/thiht/shell-scripts-matter

    set -euo pipefail
    # -e exit if any command returns non-zero status
    # -u prevent use of undefined variables
    # -o pipefail force pipelines to fail on first non-zero code

    IFS=$'\n\t'

    action=${1:-"default value"}

    readonly LOG_FILE="$(basename "$0").log"
    info() { echo "[INFO] $@" | tee -a "$LOG_FILE" >&2 ; }
    warning() { echo "[WARNING] $@" | tee -a "$LOG_FILE" >&2 ; }
    error() { echo "[ERROR] $@" | tee -a "$LOG_FILE" >&2 ; }
    fatal() { echo "[FATAL] $@" | tee -a "$LOG_FILE" >&2 ; exit 1 ; }

    cleanup() {
    info "### finished"
    }

    if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
    trap cleanup EXIT
    info "### starting"
    # Script goes here
    # ...
    fi