Tweeting from the command line

21 April 2010 17:22

I read an article over at OS X Daily about posting a tweet from the command line using the twitter API. Taking this one step further here’s a handy little script that’ll even stop you posting nothing and stuff that’s too long.

#!/bin/bash
TWEET=$1
TWEETLEN=${#TWEET}
if [ $TWEETLEN -eq 0 ] || [ $TWEETLEN -gt 140 ]; then
	if [ $TWEETLEN -gt 140 ]; then
		let EXTRA=$TWEETLEN-140
		echo "Usage: tweet \"message\" (140 chars or less, you're $EXTRA over)"
	else
		echo "Usage: tweet \"message\" (140 chars or less)"
	fi
	exit 1
else
	curl -u username:password -d status="$1" http://twitter.com/statuses/update.xml
fi
exit 0

Save the file as “tweet” and make sure it’s in the path with executable permissions of 755.

chmod 755 tweet

Filed: Technology // Tagged: , ,

You can follow any responses to this entry through the RSS 2.0 feed.

You can leave a comment or leave a trackback from your own site.

2 Responses to “Tweeting from the command line”

  1. Post a Twitter update via the command line - OS X Daily on April 22nd, 2010 at 7:02 pm

    [...] Ian Winter took the above bash script a bit further and added the ability to prevent you from posting no tweet, and a warning if a tweet is over the 140 character limit. Here is his script: #!/bin/bash TWEET=$1 TWEETLEN=${#TWEET} if [ $TWEETLEN -eq 0 ] || [ $TWEETLEN -gt 140 ]; then if [ $TWEETLEN -gt 140 ]; then let EXTRA=$TWEETLEN-140 echo "Usage: tweet "message" (140 chars or less, you're $EXTRA over)" else echo "Usage: tweet "message" (140 chars or less)" fi exit 1 else curl -u username:password -d status="$1" http://twitter.com/statuses/update.xml fi exit 0 Like before, edit your username and password, and save the file as tweet and be sure to make it executable chmod 755 tweet [...]

  2. Will on April 22nd, 2010 at 7:03 pm

    Very cool, I like it! I added this script to David’s original post on the topic with a reference and link to this.

Leave a Reply

 
What's ianteresting?   •   Twitter   •   About   •   Contact
©2012 Ian Winter. All Rights Reserved.   •   Powered by WordPress   •   Hosted at Memset