################################################################################
# Function: create_temp_dir
# Description: Creates a temporary directory
# Arguments: $1 :- temporary directory name prefix (required)
# Outputs: Prints name of the created temporary directory
function create_temp_dir {
local prefix="${1:?'create_temp_dir requires a 1st argument: prefix'}"
local temp_dir=''
until [[ -n "${temp_dir}" && ! -d "${temp_dir}" ]]; do
temp_dir="/tmp/${prefix}${RANDOM}${RANDOM}${RANDOM}"
done
if ! sudo mkdir --parents --mode 0700 "${temp_dir}"; then
echo "FATAL: Unable to create temp dir: ${temp_dir}: $?" >&2
exit 1
fi
echo "${temp_dir}"
trap 'rm --force --recursive "${temp_dir}"' ABRT EXIT HUP INT QUIT
}