bin/tmp

72 lines
1.2 KiB
Plaintext
Raw Normal View History

2018-09-16 12:48:35 -05:00
#! /bin/bash
#
# tmp.bash
# Copyright (C) 2018 salt <salt@lap-th-e560-0>
#
# Distributed under terms of the MIT license.
#
2018-10-04 16:20:38 -05:00
# Define variables
2018-09-16 12:48:35 -05:00
name="$(basename "$0" .sh)"
tmpdir="$(mktemp -d)"
2019-01-23 14:49:16 -06:00
2018-10-04 16:20:38 -05:00
# Verify them
2019-01-23 14:49:16 -06:00
[ -z "$tmpdir" ] && exit 2
2018-10-04 16:20:38 -05:00
# Define functions
2019-01-23 14:49:16 -06:00
log() {
[ -z "$1" ] && return 1
2018-11-22 02:43:48 -06:00
# shellcheck disable=1117
printf "%b: %s\n" \
"$name" \
"$1"
2018-09-16 12:48:35 -05:00
}
2019-01-23 14:49:16 -06:00
cleanup() {
[ -z "$tmpdir" ] && exit 2
log "Cleaning up \"$tmpdir\""
rm -rf "$tmpdir"
2018-10-04 16:20:38 -05:00
}
2019-01-23 14:49:16 -06:00
# 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
;;
*)
2019-02-09 20:12:37 -06:00
log "Invaild argumenet: -$OPTARG"
log "See $name -h for help"
2019-01-23 14:49:16 -06:00
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
2018-10-04 16:20:38 -05:00
trap "cleanup" EXIT
# Do the do
2019-01-23 14:49:16 -06:00
log "This directory will be removed when this shell exits"
2018-09-16 12:48:35 -05:00
(
2019-01-23 14:49:16 -06:00
cd "$tmpdir" || exit 50
2018-11-22 02:43:48 -06:00
exec "$SHELL"
2018-09-16 12:48:35 -05:00
)