Skip to content

Instantly share code, notes, and snippets.

View izabera's full-sized avatar

Isabella Bosia izabera

  • General System
  • Flitwick, UK
View GitHub Profile
@izabera
izabera / 0.README.md
Last active March 22, 2024 18:22
tput codes

This is the table from man 5 terminfo.

Do NOT hardcode terminal escape sequences. Use tput with the cap-names from the table below to get the right code for your terminal.

(P) indicates that padding may be specified

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 / ircsed
Last active June 21, 2023 14:49
a simple irc bot written in sed
#!/bin/bash
owner=izabera nick=sedirc network=irc.freenode.net
echo "
# send bot data
1 {
s/.*/nick $nick/p
s/.*/user $nick $nick $nick :$nick/p
s/.*/cat factoids 2>\/dev\/null/e; H
b
@izabera
izabera / asciinema-to-scriptreplay
Last active March 23, 2023 12:32
convert between asciinema and scriptreplay
#!/bin/bash
exec {times}> times {typescript}> typescript < "${1-/dev/stdin}"
while read -r; do [[ $REPLY = ' "stdout": [' ]] && break; done # skip to this line
LANG=C
printf "Script started on %(%c)T\n" -1 >&"$typescript" # dummy
while read -r open; [[ $open = '[' ]]; do
read -r elapsed; read -r string; read -r close
eval printf %b%n "$string" characters >&"$typescript" # put count in $characters
printf "%s %s\n" "${elapsed%,}" "$characters" >&"$times"
done
@izabera
izabera / bfsh
Created November 23, 2016 10:23
brainfuck interpreter in posix sh
#!/bin/sh
LANG=C
compile() {
# some preprocessing and basic optimizations
code=$(printf %s "$1" |
(tr -dc '\133\135<>,.+-'; echo) | # ksh's builtin tr doesn't like []<>,.+-
sed -e :a -e 's/<>//g;s/><//g;s/+-//g;s/-+//g
s/\[-]/z/g;s/zzz*/z/g;s/[+-]z/z/g;ta')
@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 &
@izabera
izabera / reverse
Last active June 13, 2022 22:18
array manipulation functions
#!/bin/bash
reverse () {
# $1 = name of a non sparse array
# $2 = index of first element
# $3 = index of last element
#(( $# != 3 )) && return 1
# expand the values, save in a local array
#eval local array=("\"\${$1[@]}\"")
@izabera
izabera / insensitive
Last active June 4, 2022 00:53
Case insensitive bash
#!/bin/bash
# source this to make your shell "case insensitive"
insensitive=(
# reserved words
case coproc do done elif else esac fi for function if in select then time until while
# builtin commands
alias bg bind break builtin caller cd command compgen complete compopt continue declare
dirs disown echo enable eval exec exit export false fc fg getopts hash help history jobs
kill let local logout mapfile popd printf pushd pwd read readarray readonly return set
shift shopt suspend test times trap true type typeset ulimit umask unalias unset wait
#include <ctype.h>
#include <fcntl.h>
#include <linux/openat2.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>