Home
Softono

Bash Utils

Open source MIT Shell
309
Stars
31
Forks
0
Issues
4
Watchers
5 months
Last Commit

 About Bash Utils

A collection of hand-crafted bash scripts for various common tasks.

Platforms

Web Self-hosted Cloud

Languages

Shell

Links

Need Help Installing Bash Utils?

We provide expert installation service for this software. Our team will install, configure, and secure Bash Utils on your server. plans start at just $30.

Bash Utils

View on GitHub

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.

When sourcing helpers directly, source util/base.sh before util/logging.sh and the other util/ or lib/ files. base.sh enables the shared shell options and defines helpers like REQUIRES_CMDS, which logging.sh depends on.

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

https://news.ycombinator.com/item?id=44096395 more lore around timeout

until

until curl --silent --fail-with-body 10.0.0.1:8080/health; do
	sleep 1
done

https://heitorpb.github.io/bla/timeout/

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

perl -pE: Best simple find-and-replace regex

echo '0.0.0.0:443->443/tcp' | perl -pE 's/0.0.0.0:(\d+)->.*/$1/gm'    # 443

sed: Truncate strings with ... ellipsis

# Example: Truncate if longer than 15 characters
echo "short string"     | sed 's/\(.\{15\}\).*/\1.../'       # short string
echo "some long string" | sed 's/\(.\{15\}\).*/\1.../'       # some long st...

# Bonus: get terminal width in columns
TERMINAL_WIDTH=$(tput cols)

Use strace to catch syscall failures

strace -e trace=clone -e fault=clone:error=EAGAIN

https://medium.com/@manav503/using-strace-to-perform-fault-injection-in-system-calls-fcb859940895