Create a Twitter Info Server with Bash

by Mike on July 23, 2009 · 1 comment

in Regular Expressions

Using Bash Shell Scripts for Twitter Updates and Information Posting
These scripts are used to both send tweets from the command line and to automate information to your Twitter account.  You can use your Linux server or desktop  to help present useful information to your account, not Spam.  This creates a way to present tips, FAQ, and other information to people as they follow your account.

Tweet Message
The first example is a bash script that you can save in your home directory, say call it twitter.sh and then chmod 755 the script.  Here is how the script works.  The two variables at the start, are where you will need to enter your email address and then your password.  These variables are called later in the script.  The “URL” variable is used to connect to the twitter account and is used later in the script.  The “result” line uses curl to send the username and password to twitter to connect to the account and then the status message is what is tweeted on your account.

################################
#!/bin/bash
# tweet_mess.sh  Tweet from your Linux account

USERNAME=”email_or_username”
PASSWORD=”password”

URL=http://twitter.com/statuses/update.xml
# Post to Twitter.
result=`curl -u $USERNAME:$PASSWORD -d status=”This is where you will place your message for Twitter” $URL`

exit 0
################################

Tweet Alias
The second illustration is when you want to create an alias in your .bash_profile so that you can use a simple command and send text anytime from the command line.  Open your .bash_profile, this is a hidden file in the user’s directory, and edit it and place this line to create an alias:

alias tw”home/user_name/tweet_com.sh’

Now you will need to edit this file tweet_com.sh  and place your username and password in it .  You will not make any other modifications to the script, except to make sure it is executable.

chmod 755 tweet_com.sh

Make sure your .bash_profile is ready with:
cd ~
source .bash_profile

Now from the command line just do this:

tw “Send a message from the command line”

################################
#!/bin/bash
# tweet_com.sh Tweet from the command line

USERNAME=”email_or_username”
PASSWORD=”password”

URL=http://twitter.com/statuses/update.xml
# Post to Twitter.
result=`curl -u $USERNAME:$PASSWORD -d status=”$1″ $URL`

exit 0
################################

Tweet Server
The Tweet Server provides you a way to create a script that will provide information to your users throughout the day.  Note I said information, not Spam.  The purpose is to give people free information that they can collect and use.  It also provides a way for people to understand what your business is about or what you are about then they are making decisions if they want to follow you.  In this example, two files were created.  First, you can use the tweet_com.sh script in the above example and then send messages to that script.  The second script reads a file which you will create that has one tweet per line.  Wherever you place your scripts make sure you have the path correct and the path to the file called “tweetfile”.  The sleep 3600 make sure you only tweet one line per hour, adjust that to your needs.

################################
#!/bin/bash
# tweet_com.sh Tweet from the command line

USERNAME=”email_or_username”
PASSWORD=”password”

URL=http://twitter.com/statuses/update.xml
# Post to Twitter.
result=`curl -u $USERNAME:$PASSWORD -d status=”$1″ $URL`

exit 0
################################

################################
#!/bin/bash
# tweet_server.sh Reads a file to send to the tweet_com.sh
FILE=tweetfile

while read LINE
do
/home/user_name/scripts/tweet_com.sh “$LINE”
sleep 3600

done < $FILE

exit 0
################################

Tweet server Option
The disadvantage of the Tweet Server using sleep 3600 is that the script is running continually and so the resources, though small are really not used correctly.  It would be better to use a crontab to run the script whenever you need to and then close it down.  Here is an option that will do just that.  This option will read one line, tweet the line, then delete the line by using sed to delete the first line and saving it as a temporary file.  Then the temporary file is moved to replace the old file.  This way you will not repeat any tweets that you set up.

################################
#!/bin/bash
# tweet_server.sh Reads deletes line and replaces file

FILE=tweetfile

read LINE <$FILE

/root/scripts/tweet_com.sh “$LINE”

sed ’1d’ <$FILE > tweettmp
sleep 2
mv tweettmp tweetfile

exit 0
################################

{ 1 comment }

Simone Brustvergrößerung August 1, 2010 at 7:48 pm

Hi great script,
but it isn’t work. No idea, Perhaps twitter has change something or work at the system?
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 131 100 112 0 19 118 20 –:–:– –:–:– –:–:– 455

i dont know where i can see is tweet update done.

Previous post:

Next post: