On 13/08/2023 19:39, Markus Läll wrote:
Hi list,
my first post here :)
As when using mktemp I've often wanted the following features:
Instead of a random string, add an incrementing number to the end of
the temp file. E.g with `mktemp -n` one would get /tmp/tmp.1,
/tmp/tmp.2 and so on. Would this make sense as a feature? (Does this
exist in some other commonly available command?) Thus far I've
resorted to a while loop in bash[1] which constructs such a numbered
file and tests for file existence, returning the first one not found,
but this is long to write out. I've also had a bash function for that,
but it seems like it would be nice if mktemp had this built in.
A similar request recently was for similar numbered extension for cp --backup
https://lists.gnu.org/archive/html/coreutils/2023-07/msg00022.html
The second feature would be to echo the temporary file name into
stderr as well. Motivating example would be when running mktemp as
part of another command, e.g `tee -a "$(mktemp -e)"` (-e is an
invented flag for this functionality), where it would print a line
"mktemp: /tmp/tmp.a0vC8d1KIt" to stderr, so that the user would know
which file was created. This is useful when a file is temporary but
you might still want to inspect it for debugging purposes later on. As
with everything, this effect can also be achieved with a longer
command[2], but it would more convenient if it were built in. :)
Shell support for redirecting output seems sufficient for this TBH
[1]
mktemp_n() (
BASENAME="$1"
SUFFIX=1
while [ -e "${BASENAME}.${SUFFIX}" ]; do
((SUFFIX++))
done
echo "${BASENAME}.${SUFFIX}"
)
[2] mktemp | (echo -n 'mktemp: ' > /dev/stderr && tee /dev/stderr)
cheers,
Pádraig