Tweeting from the command line
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






[...] 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 [...]
Very cool, I like it! I added this script to David’s original post on the topic with a reference and link to this.