Skip to content

pirate/bash-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bash-utils

A collection of my hand-crafted bash scripts and helper functions for various common tasks.

Code Layout

  • bin/ is a collection of finished scripts, for doing everything you need to do related to a specific task (e.g. dns can both set and fetch DNS values from a variety of providers)
  • lib/ is a collection of adapters to interact with 3rd party tools or scripts, e.g. cloudflare/letsencrypt/etc
  • util/ is a collection of pure bash functions to make development in bash easier e.g. logging/configuration/error handling/etc.

Reading List

For a list of my favorite CLI utilities for Linux/macOS, and much more, see here:

https://docs.sweeting.me/s/system-monitoring-tools ⭐️

For my Fish shell functions, snippets, and reading list see here:

https://github.com/pirate/fish-functions

Manpages and CLI Explainer Tools

Articles, Tools, and More

If any of these links are down, see https://archive.sweeting.me or https://archive.org for mirrors.


Useful Helper Commands

Unofficial Bash Strict Mode

#!/usr/bin/env bash

### Bash Environment Setup
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
# set -o xtrace
# set -x
# shopt -s nullglob
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
IFS=$'\n'

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

timeout

timeout executes the ssh command (with args) and sends a SIGTERM if ssh doesn't return after 5 second. for more details about timeout, read this document: http://man7.org/linux/man-pages/man1/timeout.1.html

timeout 5 some-slow-command

# or on mac:
brew install coreutils
gtimeout 5 some-slow-command

nohup

expect

trap

getopts / argbash

# parse and handle passed CLI arguments sanely
while getopts ":mnopq:rs" Option
do
  case $Option in
    m     ) echo "Scenario #1: option -m-   [OPTIND=${OPTIND}]";;
    n | o ) echo "Scenario #2: option -$Option-   [OPTIND=${OPTIND}]";;
    p     ) echo "Scenario #3: option -p-   [OPTIND=${OPTIND}]";;
    q     ) echo "Scenario #4: option -q-\
                  with argument \"$OPTARG\"   [OPTIND=${OPTIND}]";;
    #  Note that option 'q' must have an associated argument,
    #+ otherwise it falls through to the default.
    r | s ) echo "Scenario #5: option -$Option-";;
    *     ) echo "Unimplemented option chosen.";;   # Default.
  esac
done

trap

#!/bin/bash
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
function finish {
  rm -rf "$scratch"
}
trap finish EXIT

pkill

# kill any processes matching given regex
pkill nginx

eval

a='$b'
b='$c'
c=d

echo $a             # $b
                    # First level.
eval echo $a        # $c
                    # Second level.
eval eval echo $a   # d
                    # Third level.

exec

# This shell builtin replaces the current process with a specified command
# useful for when the last command in a script is a long running process you want to kick off, and you dont want it to be a child of bash

exec some-daemon-that-runs-forever

dpkg -s <pkgname> / dpkg --compare-versions "20.04.2" "ge" "18.04.12"

check pkg info of any installed package, and compare semver/date/incremental versions easily

pipeexec

Have total control over piping between processes, including doing crazy things like piping a processes own stdout into its stdin, launching complex directed graphs of pipes as a single process, etc.

https://github.com/flonatel/pipexec