Skip to content

Instantly share code, notes, and snippets.

@dviererbe
Created May 15, 2023 09:20
Show Gist options
  • Save dviererbe/64c3635f25b248a711a7cbd3c019a1f5 to your computer and use it in GitHub Desktop.
Save dviererbe/64c3635f25b248a711a7cbd3c019a1f5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
function PrintUsage
{
echo "Description:"
echo " Creates a temporary directory name that is not taken before running"
echo " this command. Works simillarly to mktemp but with incremental number"
echo " suffix (e.g. tmp_1, tmp_2, ...)"
echo ""
echo "Usage:"
echo " $0 [-h|--help] [-p|--prefix <prefix>]"
echo ""
echo "Options:"
echo " -h, --help Shows this usage info."
echo " -p, --prefix <prefix> The Prefix of the directory name that should"
echo " be used. (default: 'tmp_')"
echo ""
}
function LogErrorMessageAndExit
{
if [[ "$TERM" == *xterm* || "$TERM" == *xterm-color* ]]; then
printf "\033[0;31m%s\033[0m\n" "$1" >&2
else
echo "$1" >&2
fi
echo ""
PrintUsage
exit 1
}
prefix="tmp_"
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
PrintUsage
exit 0
;;
-p|--prefix)
if [[ $# -lt 2 ]]; then
LogErrorMessageAndExit "'$1' Option specified, but no prefix found!"
fi
prefix=$2
shift 2
;;
*)
LogErrorMessageAndExit "Unknown Option '$1'"
;;
esac
done
count=0
while true; do
directoryName="${prefix}${count}"
if [[ ! -d "$directoryName" ]]; then
mkdir "$directoryName"
echo "$directoryName"
break
fi
((count++))
done
@dviererbe
Copy link
Author

dviererbe commented May 15, 2023

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004
 
Copyright (C) 2023 Dominik Viererbe <hello@dviererbe.de>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
 
           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 0. You just DO WHAT THE FUCK YOU WANT TO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment