i'm writing another lil cube thing. if you've ever written a rubik's cube simulator/solver, you will know that some parts can be more akin to data than to code. one way or another, you always end up with lots and lots of tables to select the right pieces and move them to the right places. if good code reads like poetry and bad code reads like a phonebook, this is always easily in the top 10 most mind-numbingly boring programs of all time.
namerefs (introduced in bash 4.0) act as aliases for other variables
var=meow
declare -n ref=var
echo $ref # prints meow
ref=moo
echo $var # prints moo
1 | |
/sent/ { stot += sent[s++] = $2 } | |
/recv/ { rtot += recv[r++] = $2 } | |
END { | |
if (r != s) | |
printf "error: s=%d r=%d\n", s, r | |
for (i in sent) | |
if (sent[i] < recv[i]) | |
printf "error: sent[%d]=%d recv[%d]=%d\n", i, sent[i], i, recv[i] | |
printf "sent=%'d avg=%'d\n", stot, stot/s |
#!/bin/bash | |
usage () { | |
cat << 'eof' | |
bgcmd manages a long running, interactive command in background | |
usage: | |
bgcmd START command [args...] # starts a command in background |
# try catch for bash/mksh/zsh/dash/busybox | |
[ "$BASH_VERSION" ] && shopt -s expand_aliases | |
alias try='tryblock ()' | |
alias throw='return' | |
alias catch='if tryblock; then :; else ' | |
alias end_try='fi' | |
alias exceptions:='case $? in x) ' | |
alias except=';;' | |
alias end_exceptions='esac' | |
alias always=' ' |
for the latest chapter of what's becoming a blog on the most cursed bash you can imagine, let's do some maths together
euclid's algorithm for gcd could be written like this in python:
>>> def gcd(a, b):
... if b:
... return gcd(b, a%b)
... return a
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]
#if 0 | |
#define _XOPEN_SOURCE 700 | |
#include <aio.h> | |
#include <arpa/inet.h> | |
#include <assert.h> | |
#include <_Complex.h> | |
#include <ctype.h> | |
#include <dirent.h> | |
#include <dlfcn.h> | |
#include <errno.h> |
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 |