Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I am trying to send a random files of mail in the mail folder. The mail folder has thousand of mails and I would like to randomly take files in there and send it. I have the following sendmail command, but don't know how I can send any random file from mail folder.
sendmail -Ad -i -f root@localhost you@smtp.net < FILENAME

This script will choose a random file from a directory. (I'll leave the actual sendmail command to you). I use the ksh/bash RANDOM variable to generate a random number. Below, generating $s1 & $s2 to seed RANDOM might look confusing, but I do that to increase the "randomness":
#!/bin/ksh
# set your own directory
# cd /mydir# send the directory contents to a file one per line
myfile="/tmp/myfile"
ls -1 > $myfile# how many files are there
counter=$(wc -l < ${myfile})# seed the RANDOM number generator
s1=$(/usr/bin/ps -elfy)
s2=$(/usr/bin/cat -s /etc/shadow)
RANDOM=$(echo $s1 $s2|/usr/bin/cksum|/usr/bin/cut -b1-8)# generate a random number up to the total number of files
number=$(($RANDOM % $counter + 1))
echo $number# print the line number from the file
sed -n "${number}"p ${myfile}

RANDOM is a shell environmental variable that each time it is accessed returns a random number between 0 and 32767:
echo $RANDOM
17110Do a man on ksh for more information.
You can provide your own "seed" for RANDOM which i did with:
RANDOM=$(echo $s1 $s2|/usr/bin/cksum|/usr/bin/cut -b1-8)
I'm paranoid; probably you get by just fine with the default seed.
Next, once I have a total count of lines in the file, dividing the RANDOM number by the total number of lines in the file and looking at the remainder (MODULO):
number=$(($RANDOM % $counter + 1))
returns a number from 1 to the number of total lines - 1. Add 1 or you'll statistically never be able to look at the last line of the file.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |