#!/bin/bash ## # THIS SOFTWARE IS DISTRIBUTED UNDER GPL LICENSE # # Copyright (C) 2009 Ankur Shah # http://ankurshah.net # $comments > ashah (contactankurshahnet) # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA or visit http://www.gnu.org/licenses/gpl.txt. ## # ...system essentials CURL=${CURL:-/usr/bin/curl} TW_UID=${TW_UID:-"ankur_shah"} TW_PWD= TW_URL=http://twitter.com/statuses/update.xml TW_MAX_LEN=140; TW_MESSAGE="$@" MSG_LENGTH=`echo ${#TW_MESSAGE}` # ...routine to log messages to STDOUT function log { echo "[$$ `date \"+%Y-%m-%d %H:%M:%S\"`] $@" >&1 } # ...routine to log messages to STDERR. Exits system with $error_code function error { error_code=$1 shift echo "[$$ `date \"+%Y-%m-%d %H:%M:%S\"`] ERROR: $@" >&2 log "==== EXIT with code $error_code ====" exit $error_code } # ...routine to display usage and exit function usage { exit_code=$1 echo "Usage: $0 " exit $exit_code } # ...main routine function main { if [ $MSG_LENGTH -eq 0 ]; then usage 1 elif [ $MSG_LENGTH -gt ${TW_MAX_LEN} ]; then error 2 "Message length [${MSG_LENGTH}] longer than the allowed ${TW_MAX_LEN} characters limit" else log "Message length: ${MSG_LENGTH}" fi stty -echo echo -n "Please enter your twitter password: " read TW_PWD stty echo log "Posting message" result=`${CURL} -u ${TW_UID}:${TW_PWD} -d status="${TW_MESSAGE}" ${TW_URL}` log "Got result [${result}]" } main