Posts tagged Generate Unique file name in bash
How to generate temporary file name in bash using RANDOM environment variable
Oct 17th
Whenever the $RANDOM environment variable is accessed, it gives a random number. This can be used to generate a random file name. In the following example randfile function takes prefix and suffix of the temporary file name and returns a temporary file name after checking for its existence.
#!/bin/bash
function randfile
{
while [ 1 ];
do
TNAME="$1""$RANDOM""$2"
if [ ! -f $TNAME ]; then
echo $TNAME
exit 0
fi
done
}
NAME=`randfile /tmp/test- .tmp`
echo $NAME


Recent Comments