Created
May 15, 2023 09:20
-
-
Save dviererbe/64c3635f25b248a711a7cbd3c019a1f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
dviererbe
commented
May 15, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment