#!/bin/bash # 0.0.1 - Initial adaptation from # http://www.fsckin.com/2008/03/19/twittering-from-the-command-line/ # 0.0.2 - Protect against errors when using: # cl-twit "This isn't going to cause an error." # The double quotes are needed because of the '. # The quotes introduce spaces into $1, so it gets wrapped with "" # 0.0.3 - Don't print JSON returned by twitter.com # - Add verbose option to report # chars used/left. # - Discard account and message environment variables after used. TWITMAX=140 TWITFILE=~/.twitter TWITFILESECURE=unknown TWITVERBOSE=false # Check verbose flag if [ "x$1" == "x-v" ]; then TWITVERBOSE=true shift fi # Can't post an empty status if [ -z "$1" ]; then echo You have to say SOMETHING! exit fi # Build message and get it's length TWITMESSAGE=`echo $@|tr ' ' '+'` TWITLENGTH=`echo -n $TWITMESSAGE|wc -c` # Report characters used if being verbose if [ $TWITVERBOSE == "true" ]; then echo $TWITLENGTH used, $(($TWITMAX-$TWITLENGTH)) spare fi # Don't post if it's just going to get truncated if [ $TWITLENGTH -gt $TWITMAX ]; then echo "Message is too long! $TWITLENGTH is over the $TWITMAX limit" exit fi # Is the account settings file secure if ( stat $TWITFILE | grep '^Access: (..00/' > /dev/null ) ; then TWITFILESECURE=yes fi # Don't let anything work if we aren't secure if [ $TWITFILESECURE != "yes" ]; then echo The file $TWITFILE should not be world readable echo Try: chmod go-rwx $TWITFILE exit fi # Load account settings . $TWITFILE # Username is a mandatory setting if [ -z $TWITUSER ]; then echo TWITUSER not set in $TWITFILE exit fi # Password is a mandatory setting if [ -z $TWITPASS ]; then echo TWITPASS not set in $TWITFILE exit fi # Dispatch message to twitter curl --basic --user "$TWITUSER:$TWITPASS" \ --data-ascii "status=$TWITMESSAGE" \ --silent "http://twitter.com/statuses/update.json" # Paranoia # I don't know that these could be recovered, but you never know. unset TWITUSER TWITPASS TWITMESSAGE