This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Linear congruential generator, range 0 - 32767 | |
| # See: https://rosettacode.org/wiki/Linear_congruential_generator | |
| # Usage: lcgrng (optional: number count, default:1) (optional: seed, default: epoch in seconds) | |
| rnCount="${1:-1}" | |
| rnSeed="${2:-$(date +%s)}" | |
| # Here's an unnecessary nod to FreeBSD's rand.c | |
| if [ "${rnSeed}" = 0 ]; then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| # If the special variable isn't available (e.g. dash shell), we fall back to a BSD-style | |
| # Linear congruential generator (LCG) which we use to create our own '$RANDOM' variable | |
| # See: https://rosettacode.org/wiki/Linear_congruential_generator | |
| # If two invocations of RANDOM are the same (or both are blank), | |
| # then we're working with a shell that does not support $RANDOM. | |
| if [ "${RANDOM}" = "${RANDOM}" ]; then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #set -x | |
| # Function to generate a reliable seed for whatever method requires one | |
| # Because 'date +%s' isn't entirely portable, we try other methods to get the epoch | |
| generate_seed() { | |
| # First we check if /dev/urandom is available. | |
| if [ -c /dev/urandom ] && command -v od >/dev/null 2>&1; then | |
| # Get a string of bytes from /dev/urandom using od | |
| od -N 4 -A n -t uL /dev/urandom | tr -d '\n' | awk '{$1=$1+0};1' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| host2ip() { | |
| # If $1 is missing, print brief usage | |
| if [[ -n "$1" ]]; then | |
| printf '%s\n' "Usage: host2ip [hostname|ip.add.re.ss]" | |
| return 1 | |
| # If $1 appears to be a node name, figure out its IP | |
| elif [[ "$1" =~ '^[a-zA-Z]$' ]]; then | |
| host -4 -W 1 "$1" | awk '{print $4}' | |
| # If $1 appears to be an IP address, try to figure the reverse | |
| elif [[ "$1" =~ '^((25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})[.]){3}(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9]{1,2})$' ]]; then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Function to figure out daylight savings dates for the current year | |
| dst() { | |
| local TZ | |
| # RHEL7 / systemd | |
| if command -v timedatectl >/dev/null 2>&1; then | |
| TZ=$(timedatectl status | awk -F ': ' '/Time zone/{print $2}' | awk '{print $1}') | |
| # RHEL5, RHEL6 and similar aged CentOS/Fedora/etc | |
| elif [[ -r /etc/sysconfig/clock ]]; then | |
| TZ=$(awk -F "=" '/ZONE/{print $2}' /etc/sysconfig/clock) | |
| # Older Debian family |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if ! command -v cowsay &>/dev/null; then | |
| cowsay() { | |
| local msg | |
| if [[ -t 0 ]] && [[ -z $1 ]]; then | |
| msg="Moo! What should I say?" | |
| elif [[ -n $1 ]]; then | |
| msg="$*" | |
| else | |
| msg=$(paste -sd ' ' - | sed -e "s/[[:space:]]\\+/ /g") | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pretty () { | |
| while read -r; do | |
| printf "\033[38;5;%dm%s\033[0m\n" $(($RANDOM%255)) "${REPLY}"; | |
| done | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| date() { | |
| case "$@" in | |
| (*"%o"*) | |
| declare -a args1 | |
| declare -a args2 | |
| while [[ -n "$1" ]]; do | |
| args2+=("$1") | |
| if [[ "${1:0:1}" != + ]]; then | |
| args1+=("$1") | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| getAD-User-Info() { | |
| local userName adUserName | |
| userName="${1:?Username not supplied}" | |
| # Poll 'id' to either ensure the info is cached | |
| # or to error out if the user doesn't exist | |
| if ! id "${userName}" &>/dev/null; then | |
| printf -- '%s\n' "User '${userName}' could not be found" | |
| return 1 | |
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Because you never know what crazy systems are out there | |
| if ! command -v apropos >/dev/null 2>&1; then | |
| apropos() { man -k "$*"; } | |
| fi | |
| # A small function to trim whitespace either side of a (sub)string | |
| # shellcheck disable=SC2120 | |
| trim() { |
OlderNewer