#! /bin/bash # # tmp.bash # Copyright (C) 2018 salt # # Distributed under terms of the MIT license. # # Define variables name="$(basename "$0" .sh)" tmpdir="$(mktemp -d)" # Verify them [ -z "$tmpdir" ] && exit 2 # Define functions log() { [ -z "$1" ] && return 1 # shellcheck disable=1117 printf "%b: %s\n" \ "$name" \ "$1" } cleanup() { [ -z "$tmpdir" ] && exit 2 log "Cleaning up \"$tmpdir\"" rm -rf "$tmpdir" } # Handle args while getopts ":h" opt; do case $opt in h) cat << EOF Usage: $name [TEMPLATE] Create a temporary directory, copying the folder [TEMPLATE] from ~/Templates/tmp, if it exists. -h Show this help text https://gitlab.com/rehashedsalt/bin EOF exit 0 ;; *) log "Invaild argumenet: $OPTARG" exit 1 ;; esac done # Copy in template directory if [ -n "$1" ]; then copydir="$HOME/Templates/tmp/$1" if [ -d "$copydir" ]; then log "Using template \"$1\"" cp -r "$copydir"/* "$tmpdir" else log "Could not find template directory \"$copydir\"" fi fi trap "cleanup" EXIT # Do the do log "This directory will be removed when this shell exits" ( cd "$tmpdir" || exit 50 exec "$SHELL" )