Computing.Net > Forums > Unix > send random file (sendmail)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

send random file (sendmail)

Reply to Message Icon

Name: lndsey
Date: February 26, 2008 at 05:44:55 Pacific
OS: unix
CPU/Ram: none
Product: none
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 27, 2008 at 09:50:33 Pacific
Reply:

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}


0

Response Number 2
Name: lndsey
Date: February 29, 2008 at 06:05:13 Pacific
Reply:

Thanks for your help!
Can you please explain what you are doing there with random generator.

thanks


0

Response Number 3
Name: nails
Date: February 29, 2008 at 12:13:14 Pacific
Reply:

RANDOM is a shell environmental variable that each time it is accessed returns a random number between 0 and 32767:

echo $RANDOM
17110

Do 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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: send random file (sendmail)

Script that sends a file via mailx www.computing.net/answers/unix/script-that-sends-a-file-via-mailx/6063.html

shell script to send a file thru FT www.computing.net/answers/unix/shell-script-to-send-a-file-thru-ft/6428.html

scp transmits ok but random return www.computing.net/answers/unix/scp-transmits-ok-but-random-return/6759.html