sheenobu dot net

2021-06-20 - Bash - Into

Writing my nixos config bootstrap script required lots of mkdirs and pushd/popd and I was able to figure out something that both looked/structured nicely and ended up with a practical side effect that aided in testing.

into works as a mkdir and pushd together and is meant to take a relative path only. We then cd into whatever the prefix P is, which is / if it is not provided. We then have a nice ruby-esq code structure which can use whitespace for clarity.

This also means I can test and run against folders that aren't root by making setting P to one of my optional script arguments.

Exercise for the readers: reject into invocations that use non-relative paths. pop arguments off the stack so P can just be "whatever is left" after consuming args 1 through N, instead of "$3".

if [[ $# -lt 2 ]] ; then
    exit 1
mkdir -p "$P" && cd "$P"
function into() {
    mkdir -p "$1" && pushd "$1"
into etc/nixos
    # setup hg repo we push to
    into config
        test -e .hg || hg init .
← Back to /snippets