create a file with prefix =file1 followed by the random number

You want to test the existence with -f and then create a file. But what happens, if between test and creation another task creates the file? mktemp can resolve this problem:

filename=$( mktemp ./file1-XXXXXXXXXX ) || exit 1

In this case, an empty file is created (like using touch). The XXXXXXXXXX part is replaced by a random alphanumeric string. The file anme is stored into filename. On error exit 1 is executed.

Browse More Popular Posts

Leave a Comment