Skip to content

Instantly share code, notes, and snippets.

@tcely
tcely / sha256sumc.sh
Last active September 21, 2023 04:21
replace 'sha256sum -c' with openssl / cmp in bash
#!/bin/bash
sha256sumc ()
{
local err file hash out rc=0;
while IFS=' ' read -r hash file; do
file="${file#[*]}";
out="$(openssl dgst -sha256 -r "$file" 2>/dev/null)" && cmp -s <(echo "$out") <(printf -- '%s *%s\n' "$hash" "$file") && printf -- '%s: OK\n' "$file" || {
printf -- '%s: FAILED' "$file";
err="$(openssl dgst -sha256 -r "$file" 2>&1 >/dev/null)";
if [[ "$err" =~ ': No such file or directory'$'\n' ]]; then
@tcely
tcely / config
Created September 8, 2023 14:28
Turn on ssh-rsa for only the hosts that require that option
# Add this to the ~/.ssh/config file
#
Match Exec "ssh-rsa-needed.sh '%n' '%C' '%l' '%h' '%p' '%r'"
PubkeyAcceptedKeyTypes +ssh-rsa
@tcely
tcely / defensive_bash.sh
Created December 9, 2017 21:36
Clean bash aliases and functions
#!/bin/bash
# *Temporarily* force Bash into POSIX compatibility mode, where `unset` cannot
# be shadowed, which allows us to undefine any `unset` *function* as well
# as other functions that may shadow crucial commands.
# Note: Fortunately, POSIXLY_CORRECT= works even without `export`, because
# use of `export` is not safe at this point.
# By contrast, a simple assignment cannot be tampered with.
POSIXLY_CORRECT=
@tcely
tcely / history.inc.sh
Last active August 24, 2023 20:33
Bash History Configuration
if [ "${BASH_VERSION-}" ]; then
# Local variables the functions depend upon
_bash_history_prefix=~/.local/history/bash
# {{{ Begining of the temporary functions block
_bash_history_get_today() {
date '+%Y/0%m/%d'
}
@tcely
tcely / shebang.awk
Last active January 12, 2023 19:42
A good portable awk shebang is not easy to find.
#!/usr/bin/awk -f
#!/usr/bin/awk -E
#
# If you have an awk version that doesn't support the -f flag,
# then you are just out of luck.
#
# If you just have no clue where awk will be, or you prefer to use -E,
# then you can try this bash snippet to launch awk for you.
#
# You might think the -E tests below are overly complex. You'd be wrong.
@tcely
tcely / Social_Media.md
Created December 18, 2022 23:04
Social Media Information
#!/bin/sh
github_email_to_login() {
local url='https://api.github.com/search/users'
local filter='.items|.[]|select(.type == "User")|.login'
local email="${1}"
curl -sSL "${url}?q=in:email+${email}" | jq -r "${filter}"
}
@tcely
tcely / simple-echo.function.sh
Created September 22, 2020 00:04
A simple echo using the shell printf for consistent behavior
#!/usr/bin/env sh
secho() {
_arg="${1}";
_fmt='%s';
_sentinel='--';
case "${_arg}" in
(-e|-en|-ne) _fmt='%b'; shift ;;
(-n|-En|-nE) shift ;;
@tcely
tcely / colorMsg.func.sh
Created February 5, 2015 02:38
A bash function for displaying wrapped messages in ANSI terminal colors.
#!/bin/bash
#
# Requires:
# awk
# fmt
# tput
#
colorMsg() {
[ $# -gt 1 ] || return 0
tput -S <<< "$(printf '%s\n' 'sgr 0' ${1##*[0-7]} "setaf ${1%%[^0-7]*}")"
@tcely
tcely / str_hex_conversion.func.sh
Last active May 14, 2022 17:15
Bash functions for converting hexadecimal strings back to ASCII and for converting ASCII strings to hexadecimal strings.
#!/bin/bash
#
hextostr() {
local i _hs="$*"
local _hsl="${#_hs}"
printf '%s\n' "$(for ((i=0; i < _hsl; i+=2)); do echo -ne "\x${_hs:i:2}"; done)"
unset -v _hsl _hs i
}
#hextostr '48656C6C6F20776F726C6421'