Shell script to generate random files using RANDOM variable

in Tempfile

#!/bin/bash
# Shell script to generate random file method # 1
#
# This is a free shell script under GNU GPL version 2.0 or above
#
# Copyright (C) 2005 nixCraft
#
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
 
echo "List of temporary files : "
for i  in 1 2 3 4 5
do
   FILE="/tmp/$(basename $0).$RANDOM.txt"
   echo $FILE # show file name
   > $FILE # create files
done
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 2 comments… read them below or add one }

Nerf June 18, 2008 at 1:40 pm

This is bad practice. A much better way would be to use $$, which is the process ID. In the loop above I would create a filename of “/tmp/$(basename $0).$$.txt”

Reply

guy May 19, 2009 at 8:17 am

or both… /tmp/$(basename $0).$RANDOM.$$.txt

say hello to the safe side

:D

Reply

Leave a Comment

Previous post: Script to update user password in batch mode using pwgen and chpasswd

Next post: Shell Script to send HTTP Code 301 Moved Permanently Followed by Actual URL Redirection