Skip to content

Instantly share code, notes, and snippets.

@bkw777
bkw777 / bashcat.sh
Last active March 3, 2024 01:55
cat without cat
# minimal example just for reference, not pointful by itself
# binary-safe read/write file without cp/cat/dd etc, pure bash, no subshell
while LANG=C IFS= read -d '' -r -n 1 x ;do printf '%c' "$x" ;done <bin1 >bin2
@bkw777
bkw777 / BYA_BYxxxx_hot_plate.txt
Last active December 29, 2023 21:53
BYA BY1010 BY1515 BY2020 BY2030 BY3030 BY4030 hot plate directions
Directions for BYA hot plate
BY1010 BY1515 BY2020 BY2030 BY3030 BY4030
https://www.amazon.com/dp/B0932X6JB8
https://www.amazon.com/dp/B0CN19C4DC
https://www.amazon.com/dp/B0CC62TP4L
https://www.aliexpress.us/item/3256805843992076.html
https://www.aliexpress.us/item/3256805212626461.html
https://www.aliexpress.us/item/3256804646990894.html
@bkw777
bkw777 / downpour_merge.sh
Last active April 8, 2023 23:35
Merge downpour.com m4b files
#!/bin/bash
# Merges downpour.com m4b files into a single m4b file per book.
# Directions:
# download m4b files from downpour.com
# cd into the directory with "Book Title ...[File # of #].m4b
# run with no arguments
# Multiple books may all be downloaded into the same directory.
# Previously combined books may still exist in the same directory.
@bkw777
bkw777 / show_signals.sh
Created March 12, 2023 02:20
Trap & display all possible signals in bash
# with bash-isms
for s in {1..64}; do trap "echo trap $s" $s; done
# without bash-isms
s=1; while [ $s -le 64 ]; do trap "echo trap $s" $s; s=$((s+1)); done
@bkw777
bkw777 / DIP_PCB_LEGS.md
Last active December 11, 2023 14:27
DIP PCB LEGS

It's common these days to need to replace obsolete DIP chip parts in vintage electronics with small PCBs that fit where the DIP chips used to go. For instance the infamous SID and PLA chips in Commodore computers.

However the most common types of pin headers available to make the legs are actually too thick and they damage DIP sockets. This includes common machined round pins, which are thinner than the square pins, and ok for use in round sockets, but still technically a bit too thick for leaf sockets. They can compress the wipers and make the socket no longer make a good connection with a real DIP chip any more. Also those pins always have a quite large insulator & shoulder, and in some cases there is not much room where a DIP chip came out of for that much vertical thickness of the pin header shoulders and insulators plus the pcb plus the components soldered to the pcb.

Here are 3 different ways to make DIP legs on PCBs, where the legs are thin enough not to stretch out leaf/wiper style DIP sockets, and

@bkw777
bkw777 / bash_uname.sh
Last active August 7, 2021 00:15
Platform detection in bash without external executables (no /bin/uname)
#!/usr/bin/env bash
# Example to detect OS in bash using bash built-in variable $OSTYPE instead of `uname`
# platform differences
stty_f="-F" SERIAL_TTY_PREFIX=ttyUSB # Default (Linux)
case "${OSTYPE,,}" in
*bsd*) stty_f="-f" SERIAL_TTY_PREFIX=ttyU ;; # FreeBSD/NetBSD/OpenBSD/etc
darwin*) stty_f="-f" SERIAL_TTY_PREFIX=cu.usbserial- ;; # Mac OSX
esac
@bkw777
bkw777 / bin_to_hex_to_bin.sh
Last active July 7, 2022 05:04
binary-safe file-to-ram, ram-to-file, in pure bash
#!/usr/bin/env bash
# file-to-ram, ram-to-file, in pure bash
# binary-safe including nulls, no external tools, no sub-shells
# Brian K. White b.kenyon.w@gmail.com
# read file binary to h[] hex pairs
ftoh () {
local -i i= ;local x= LANG=C ;h=()
while IFS= read -d '' -r -n 1 x ;do printf -v h[i++] '%02X' "'$x" ;done <$1
}
@bkw777
bkw777 / binary_read_tty.sh
Last active August 6, 2022 23:41
Read, store, and reproduce binary data, including nulls, in pure bash with no external executables or even subshells.
#!/usr/bin/env bash
# Read binary data, including null bytes, from a serial port,
# in pure bash with no external executables nor even subshells,
# even though it's not possible to store a null in a shell variable.
# Uses globals to avoid forking subshells.
# Example, echo -e "\0\0\0" |read FOO or printf -v FOO '%b' "\0\0\0"
# FOO will not contain any 0x00 bytes.
# Same goes for mapfile/readarray.
@bkw777
bkw777 / bash_sleep.sh
Last active August 6, 2021 22:02
sleep in pure native bash without /usr/bin/sleep and without even subshell
#!/usr/bin/env bash
# sleep without externals or children
# and without the bash sleep builtin
# mkfifo is not exactly free, but you only do this part once.
# After that it is relatively free to call _sleep all you want even in tight loops.
sleep_fifo=/tmp/.${0//\//_}.$$.sleep.fifo
trap "rm -f $sleep_fifo" EXIT
mkfifo $sleep_fifo || { echo "$0: Error creating sleep fifo \"$sleep_fifo\"" >&2 ; exit 1 ; }
exec 9<>$sleep_fifo
@bkw777
bkw777 / urlencode.sh
Last active August 6, 2021 18:24
urlencode urldecode in pure bash as efficient as possible
#!/bin/bash
# urlencode / urldecode in pure bash without externals
# no backticks or forking, all in-memory ops
# urlencode/urldecode the contents of $x, place results back in $x
# allow bash-isms
# b.kenyon.w@gmail.com
urlencx () {
local LANG=C i b t=$x l=${#x} ;x=''
for ((i=0;i<l;i++)); do