Skip to content

Instantly share code, notes, and snippets.

View izabera's full-sized avatar

Isabella Bosia izabera

  • XTX
  • Flitwick, UK
View GitHub Profile
@izabera
izabera / m2g.py
Last active November 23, 2024 10:34
a basic alternative to make2graph, significantly less painful to read
#!/usr/bin/env python3
# a basic alternative to make2graph, significantly less painful to read
import re
import sys
class Node:
def __init__(self, target):
self.target = target
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 December 25, 2025 23:54
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;

This is from man bash:

A pipeline is a sequence of one or more commands separated by one of the control operators | or |&. The format for a pipeline is:

[ time [ -p ]] [ ! ] command [ [ | | |& ] command2 ... ]

This is the only place in which ! can appear. It never prefixes commands, it prefixes pipelines.

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.)