Skip to content

Instantly share code, notes, and snippets.

View apathor's full-sized avatar
💭
🐦

Mike Lalumiere apathor

💭
🐦
View GitHub Profile
#!/bin/bash
ti() {
# usage
read -r -d '' use <<EOF
USAGE: ti [OPTIONS] INTERVAL
Parse and render this friendly temporal shorthand: ([0-9]+[UNIT][SPACE]*)+
Given an integer number of seconds, print the interval in shorthand.
Given a temporal shorthand string parse it into seconds.
OPTIONS:
> ansible-inventory -i inventory/others --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"mine",
"ungrouped"
]
Your object is to save the world, while still leading a pleasant life.
You are destined to become the commandant of the fighting men of the
department of transportation.
test
# pushf STACKFILE LINE
# Treat STACKFILE as a stack, pushing LINE onto the start of the file.
pushf() {
local file="$1"; shift;
[ -n "$file" ] || return 127
declare -a stack;
[ -f "$file" ] && mapfile -t stack < "$file"
printf "%s\n" "$@" "${stack[@]}" > "$file"
}
# ; -*-shell-script-*-
# filed DIR [OUTFILE]
# Combine each file in DIR into OUTFILE.
# If a file is executable its output will be used instead of its contents.
# The files are considered in lexographic order.
filed() {
# accept a directory and require that it exists
local dir="${1}"
if ! [ -n "$dir" ] || ! [ -d "$dir" ]; then return 1; fi
# optionally accept an output file that must not exist
# ; -*-shell-script-*-
# sourced DIRECTORY [LOG]
# Source each file in given directory, logging to optional log file.
# shellcheck disable=SC1090
sourced() {
local dir="$1"
if ! [ -d "$dir" ]; then return 1; fi
local log="${2:-/dev/stdout}"
local _ it
for it in "$dir"/*; do
rtt() { local h="$1" c="${2:-1}"; if [ -z "$h" ]; then return 1; fi; printf "%s %s\n" "$h" "$(ping -W1 -c "$c" "$h" | awk '/rtt/{ gsub("/", " ", $4); print $4 }')"; };
sort-by-rtt() { mapfile hosts; while read -r h _; do rtt "$h" & done < <(printf "%s\n" "${hosts[@]}") | sort -nk2; wait; }
# sort-by-rtt < <( awk '{print $2}' ~/.hosts | shuf -n3)
rtt() {
local h="$1" c="${2:-1}" r=""
if [ -z "$h" ]; then return 1; fi
r=$(ping -W1 -c "$c" "$h" | awk '/rtt/{ gsub("/", " ", $4); print $4 }')
if [ -z "$r" ]; then return 2; fi
printf "%s %s\n" "$h" "$r"
};
sort-by-rtt() {
mapfile hosts