coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Probing suitability for two features: add incremental integer suffix; p


From: Markus Läll
Subject: Probing suitability for two features: add incremental integer suffix; print created temp file/dir to stderr
Date: Sun, 13 Aug 2023 21:39:04 +0300

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.

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. :)

[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)


Thanks for reading!


-- 
Markus Läll



reply via email to

[Prev in Thread] Current Thread [Next in Thread]