Skip to content

Instantly share code, notes, and snippets.

View izabera's full-sized avatar

Isabella Bosia izabera

  • DDN
  • Flitwick, UK
View GitHub Profile
import collections
import itertools
import signal
import os
import time
import errno
blocks = " ▁▂▃▄▅▆▇█"

you know ${var?error message here} in sh? it produces an error and prints the message if $var is undefined. otherwise it expands to $var

introducing: ${var+${error message here}}

if $var is defined, the spaces in the error message cause a syntax error, which prints the message. otherwise it expands to nothing

(and similarly for all other outer expansions)

it reliably[^reliable] works[^works] perfectly[^dashzsh] in every[^shells] posix sh[^ext]

@izabera
izabera / game.bash
Last active October 24, 2024 21:16
basic bash game loop
#!/bin/bash
# basic bash game loop
shopt -s extglob globasciiranges
gamesetup () {
exitfunc() { :; } # override this if you want something done at exit
trap exitfunc exit
if [[ -t 1 ]]; then
printf '\e[?%s' 1049h 25l
@izabera
izabera / readfile.md
Last active November 2, 2024 10:59
reading a file line by line in your shell

context: shells want to leave the seek position at the right offset so external commands can continue reading from the same fd


i saw this somewhat surprising tally of syscalls in bash. let's investigate

surprise

one iteration:

  • unblock all signals
@izabera
izabera / loop.s
Last active October 19, 2024 01:35
an 8 byte binary that does 1 syscall (exit) and that takes 8 seconds to run on my machine
.globl _start
_start:
dec %ecx # comment this out for a 6 byte binary that takes ~1090 years :)
loop:
loop loop
mov $60, %al
syscall
$ clang++ operator_symbols.cpp -c -std=c++23 -pedantic -Wall -Wextra
$ nm -C operator_symbols.o | grep meow
0000000000000000 T operator"" _meow(char const*)
0000000000000000 W meow::promise_type::final_suspend()
0000000000000000 W meow::promise_type::initial_suspend()
0000000000000000 W meow::promise_type::get_return_object()
0000000000000000 W meow::operator&&(int)
0000000000000000 W meow::operator&()
0000000000000000 W meow::operator&(int)
0000000000000000 W meow::operator&=(int)
@izabera
izabera / vivi.cpp
Last active October 7, 2024 15:57
basic autovivifying variables in c++, similar to perl/gawk/...
#include <string>
#include <unordered_map>
#include <variant>
template <typename keytype = std::string, typename valtype = std::string>
struct autovivi {
using maptype = std::unordered_map<keytype, autovivi>;
std::variant<std::monostate, valtype, maptype> valueormap;
autovivi() = default;

A funky shell thingy that I've never seen before

So you're in posix sh and you want to do the equivalent of this in bash:

foo | tee >(bar) >(baz) >/dev/null

(Suppose that bar and baz don't produce output. Add redirections where needed if that's not the case.)

@izabera
izabera / automata.sed
Last active September 8, 2023 01:03
cellular automata in sed
#!/bin/sed -f
# store rule in hold space
1 { h; d; }
# make borders wrap
s/\(.\)\(.*\)\(.\)/>\3\1\2\3\1|/
# do some mucking around that can probably be done more elegantly by someone who is actually good at sed
x; G; h; s/\n.*//; x
@izabera
izabera / snoopfd
Last active August 25, 2022 12:00
Dump what an arbitrary linux process writes to some fd
#!/bin/sh -e
target=${1?usage: $0 pid [fd]}
fd=${2-1}
fifo=fifo.$$
log=log.$$
mkfifo $fifo
echo writing to $log
tee $log <$fifo >/proc/$target/fd/$fd &