Sending Random Lines from a Textfile to Facebook&Twitter

The Script

# The activesayings.txt file is the file we are choosing our quotes from
# When a saying is chosen it is removed from the activesayings.txt file
# to avoid frequent repeat posts. When the active sayings file is
# emptied, it is refilled with the complete list, sayings.txt.
# The complete list of sayings is stored in a Dropbox folder which
# allows me to update it from multiple machines or even my Android
# phone. When I want to add new sayings to my list I put
# them in the addsayings.txt file which also resides in Dropbox. Each
# time the script runs it adds any new sayings at the end of both
# the activesayings.txt file and the also the sayings file. It finishes
# by clearing the addsayings.txt file.
# Begin code
# if activesayings.txt file is empty then reload it to start over
if [ ! -s /home/bruce/activesayings.txt ]; then
cp /home/bruce/Dropbox/sayings.txt /home/bruce/activesayings.txt
# how many sayings in the activesayings.txt file?
Y=$(wc -l < /home/bruce/activesayings.txt)
# choose one saying at random
x=$((${RANDOM} % $Y + 1))
let w=$x-1
# copy all the sayings before the selected one to file a1
head -$w /home/bruce/activesayings.txt > a1
# copy the selected saying to file a
head -$x /home/username/activesayings.txt | tail -1 > a
let z=$Y-$x
# copy all the sayings after the selected one to file a2
tail -$z /home/username/activesayings.txt > a2
# post the selected saying to posterous via gmail
mutt -s "favorite sayings" < a
# concatenate unselected sayings with any new sayings to update the
# activesayings.txt file
cat a1 a2 /home/bruce/Dropbox/addsayings.txt > activesayings.txt
# also concatenate any new sayings at the end of the sayings.txt file
cp /home/username/Dropbox/sayings.txt sayings.txt
cat sayings.txt /home/username/Dropbox/addsayings.txt > /home/username/Dropbox/sayings.txt
# empty the new sayings file we just appended
rm /home/username/Dropbox/addsayings.txt
touch /home/username/Dropbox/addsayings.txt

The command file uses wc to get line count of file filename

    wc is short for word count, it finds the number of lines, words, and characters in a file.
    wc -l myfile.txt – finds the number of lines in a file
    The RANDOM function is used to chose an random number between 1 and the the number of lines.
    head chooses the lines between 1 and the result from RANDOM. This is piped to tail -1 which chooses the last line, the line picked by RANDOM. That line is sent to file “a”.
    mutt then sends the file “a” with a title to the specified email address.

In my case the file contains a list of favorite quotations. One of the quotes is selected at random. The selected quote is then emailed to my Posterous site. Posterious posts the selected update to my Twitter and Facebook accounts.

Install and Configure Mutt

Mutt is a commandline based Mail User Agent (MUA), and was written to view mail. It was not written to retrieve, send, or filter mail. It relies on external programs to do those tasks. One approach, the one I’m using, sends and receives emails through gmail. I’m using this approach to send text based emails with the script shown above.

Begin by installing mutt with this command

sudo apt-get install mutt

Next set up IMAP to use gmail. Open gedit, click the Open button and select the file .muttrc in your home folder. (Note: files whose names begin with a “.” such as .muttrc are hidden files. So, if you don’t see your hidden files simply right click on the background of the file browser you just opened and tick the Show Hidden Files box.)

After you have opened the file, you have to tell mutt who you are. To do this, put the following in your .muttrc

set from = ""
set realname = "Your Real Name"

Then mutt has to know where your gmail mailbox is and what your password is.

set imap_user = ""
set imap_pass = "yourpassword"

After making these changes save .muttrc and exit gedit. At that point you are ready to send a test email using mutt.

mutt -s "favorite sayings" recipientemailaddress < textfiletosend

When you run mutt for the first time, a certificate will be downloaded and you will be asked if you want to keep it. Just type Yes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: