Skip to content

Instantly share code, notes, and snippets.

@ormaaj
ormaaj / asnorder.sh
Last active April 27, 2024 22:07
Array evaluation order tests
#!/usr/bin/env ksh
# Testcase runs in ksh93t or greater. Tests should run in any shell provided
# you can supply all the necessary workarounds, and they correctly interpret
# ksh93 printf %q output (requires $'...'). At least one level of recursive
# arithmetic variable evaluation must also be supported.
# Dan Douglas <ormaaj@gmail.com>
namespace main {
# e.g. add "set -x" to hacks
typeset -A shells=(
@ormaaj
ormaaj / colorset.bash
Last active December 23, 2021 05:01
set up color mappings into associative arrays
#!/usr/bin/env bash
${ZSH_VERSION+false} || emulate ksh
${BASH_VERSION+shopt -s lastpipe extglob}
# colorSet [ --setaf | --setab | --misc ] var
# Assigns the selected set of escape mappings to the given associative array names.
function colorSet {
typeset -a clrs msc
typeset x
@ormaaj
ormaaj / bashrc
Created April 15, 2013 14:53
/etc/portage/bashrc -- Magic hook functions for Gentoo
#!/usr/bin/env bash
# /etc/portage/bashrc
# Dan Douglas <ormaaj@gmail.com>
[[ $EBUILD_PHASE != setup ]] && return
# Set up the initial associative array of CFLAGS and CXXFLAGS.
# Takes an array name to assign and optionally the names of "flag groups" to include.
# setupFlags arrname groupname [ groupname ... ]
setupFlags() {
@ormaaj
ormaaj / reftest.bash
Last active March 30, 2021 13:39
Broken namerefs.
#!/usr/bin/env bash
# Only ksh93 has "real" C++-like references (called namerefs). They can be used
# as reference parameters for passing data structures like arrays in and out of
# functions. Bash 4.3+ and mksh also have a nameref feature, but unlike ksh93,
# which has three different kinds of nameref, bash supports just two of these
# and mksh only one (the for-loop type won't be illustrated here). It is the
# "dynamic" kind of nameref that is supported by all three, which is mostly
# just sugar for Bash's old "${!var}" behavior (ksh93 also uses this kind when
# a nameref doesn't refer to a positional parameter).
@ormaaj
ormaaj / puzzle
Last active December 18, 2015 07:19
Puzzle. Moderately easy difficulty.
# Exploit this vulnerable program to run an arbitrary command without renaming or adding any files.
k# ksh -c 'touch printf "you fail\n"; printf "%s: " "give me a value"; read -r b; a=(~(N)*); "${a[@]}" "$b" "$@"' _ args...; code...
@ormaaj
ormaaj / gist:5927201
Last active December 19, 2015 08:39 — forked from anonymous/gist:5926435
modified for sw
#!/bin/bash
${BASH_VERSION+shopt -s extglob lastpipe} 2>/dev/null
# Full path to a file to store our date offset data. Will be overwritten.
datefile=./datefile
# Assign your vars or insert whatever code does so here.
user=foouser password=mypassword db=mydbname
function doit {
@ormaaj
ormaaj / pattest.py
Last active July 9, 2022 17:31
Shell pattern quote/escape fuzzer.
#!/usr/bin/env python3
import subprocess, itertools
class Shell(list):
def __init__(self, shell, cmds):
self.shell = shell
super().__init__([(x, self.__run(x)) for x in cmds])
def __iter__(self):
@ormaaj
ormaaj / bash 4.2
Last active December 21, 2015 23:19
Effect of flattening vector expansions in assignments.
4.2.45(1)-release
var=${a[*]} ... one two three four
var="${a[*]}" ... one:::two:three:::four
var=$* ... one:::two:three:::four
var="$*" ... one:::two:three:::four
var=${a[@]} ... one two three four
var="${a[@]}" ... one:::two three:::four
var=$@ ... one two three four
var="$@" ... one:::two three:::four
@ormaaj
ormaaj / virtuals.cs
Last active December 22, 2015 01:29
using System;
using System.Collections.Generic;
namespace Program {
public abstract class A {
protected abstract string Name { get; set; }
public abstract void Print();
}
class B : A {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace Program {
interface Ifc1 { void Print(string s); }
interface Ifc2 { void Print(string s); }