Shell script to generate random files using RANDOM variable

by on April 10, 2008 · 2 comments

#!/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
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

{ 2 comments… read them below or add one }

1 Nerf June 18, 2008

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”

2 guy May 19, 2009

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

say hello to the safe side

:D

Leave a Comment

You can use these HTML tags and attributes for UNIX commands or shell scripts: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 12 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a script.



Tagged as: bash shell script, for loop, free shell, gnu gpl version, loop method, random file, random files, shell loops, temporary files

Previous Script:

Next Script: