Skip to content

Instantly share code, notes, and snippets.

@darkseid4nk
darkseid4nk / variables.sh
Created December 12, 2024 05:54
Helper functions for variables.
var_empty(){
# var_empty "_varname"
# var_empty "_array[index]"
# var_empty "_dict[key]"
# Returns 0 if a variable is set to an empty string
[[ -z ${!1-unset} ]] && return 0 || return 1
}
var_gettype(){
# var_gettype "_varname"
@darkseid4nk
darkseid4nk / strings.sh
Created December 12, 2024 05:52
Helper functions for strings. Parameter expansions for the win
explode(){
# explode <delimiter> <string> <array_name_to_be_referenced>
# Splits string by delimiter and fills input array where each value is each string that was deparated
# Works on arrays and dicts. Will overwrite if the array is not empty...
[ "$#" -ne "3" ] && return 1
[[ "$(declare -p -- "${3}" 2>/dev/null)" != "declare -a "* ]] && [[ "$(declare -p -- "${3}" 2>/dev/null)" != "declare -A "* ]] && return 1
local -n _arr="$3"
OLDIFS=$IFS
IFS=$1 read -ra _arr <<< "$2"
IFS=$OLDIFS; unset OLDIFS
@darkseid4nk
darkseid4nk / arrays.sh
Last active December 12, 2024 05:50
Helper functions for workin with arrays/dicts
# Most functions use direct referencing for dict compatibility and key referencing when indexes/keys may not be in a sequential numeric order.
# inarray is the only function that you expand the entire array as an argument i.e. "${_arr[@]}"
array_empty(){
# array_empty "_arrayname"
# uses direct reference
# Returns 0 if an array is empty. Returns 1 otherwise
# Works on arrays and dicts
[[ "$#" -ne "1" ]] && return 1
[[ "${!1@a}" != [aA] ]] && return 1
@darkseid4nk
darkseid4nk / ssh_control_socket.sh
Created October 2, 2024 19:54 — forked from wolever/ssh_control_socket.sh
Bash script to start and background an SSH ControlMaster to speed up SSH in the script
# Call `setup_ssh_socket` to setup the control socket (function will return once
# the socket is ready to go), and `ssh_target` will connect using the control socket.
# Assumes TARGET_HOST variable is set.
# The connection is automatically closed when the script exists.
# TARGET_HOST="wolever.net"
# setup_ssh_control_socket
# ssh_target "hostname"
debug() {
echo "DEBUG: $*"
@darkseid4nk
darkseid4nk / mkca.sh
Created October 2, 2024 19:54 — forked from wolever/mkca.sh
Tools for creating private certificate authorities
#!/bin/bash
set -e
nodes="-nodes"
if [[ "$1" == "-des" ]]; then
nodes=""
shift
fi
if [[ ! "$1" || ! "$2" || "$1" =~ "^-" ]]; then