Você está na página 1de 2

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 w ay to present tips, FAQ, and other information to people as they follow your acc ount. 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 a ddress 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 t he 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 you r 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 m essage for Twitter $URL` exit 0 ################################ Tweet Alias The second illustration is when you want to create an alias in your .bash_profil e so that you can use a simple command and send text anytime from the command li ne. Open your .bash_profile, this is a hidden file in the user s directory, and e dit 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 pa ssword 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 informa tion 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 al so provides a way for people to understand what your business is about or what y ou 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 rea ds 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 calle d tweetfile . The sleep 3600 make sure you only tweet one line per hour, adjust th at 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 runn ing 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 an d then close it down. Here is an option that will do just that. This option wi ll read one line, tweet the line, then delete the line by using sed to delete th e first line and saving it as a temporary file. Then the temporary file is move d 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 ################################

Você também pode gostar