\#\!/bin/bash
\# Author: Miles Z. Sterrett \<miles -dot- sterrett AT gmail -dot- com\>
\# Created: 03-29-08\# Updated: 03-29-08\# Sends an update to Twitter
\# Uncomment and enter your own Twitter info
\# to avoid having to use the command line parameters
\#USER=username
\#PASS=password
function force\_response {message=$1localanswer=read -e -p "$message" answer
while\[ -z $answer\]; doecho"You must enter a value to continue..."read -e -p "$message" answer
doneeval $2=$answer}whilegetopts"u:p:hm:" OPTION
docase$OPTIONin
u)USER=$OPTARG
;;
p)PASS=$OPTARG
;;
h)
usage
exit1
;;
m)MESSAGE=$OPTARG
;;
?)
usage
exit
;;
esacdoneshift $(($OPTIND-1))MESSAGE=$1function usage {
cat \<\<EOF
Usage: $0\[-h\]\[-d DOMAIN\]\[-e EMAIL\]\[-u USER\]
This script will post a message to Twitter
-h This usage information.
-m Your message
-p PASS Twitter password
-u USER Twitter user name
EOF
}if\[ -z $USER\]; then
force\_response "Enter your Twitter user name: " username
USER=$usernamefiif\[ -z $PASS\]; then
force\_response "Enter your Twitter password: " password
PASS=$passwordfiif\[ -z "$MESSAGE"\]; then
force\_response "Enter your update message: " message
MESSAGE=$messagefi
curl -u ${USER-\`whoami\`}:$PASS -d status="$MESSAGE" http://twitter.com/statuses/update.xml
exit0
I prefer to copy the script to /usr/local/bin, put my Twitter information in the script itself, and then:
tweet 'So, today, I totally wrote a bash script and stuff'
If you don’t like that, you can do something like this:
./tweet -u username -p password 'DHH and Linus are actually long lost twins. Discuss.'
If you simply execute the script, it should prompt you for the various required information.
I don’t claim to be the most skilled bash scripter on the Internets. I encourage you to let me know what you think about it, fork it and improve it, or steal it and tailor it to your use.
Also, I am well aware that I spent more time on this than simplifying that curl call was really worth. I loved every freakin’ minute of it, too.
Note: Some functions in this script, as well as much of the style, is courtesy of browsing scripts written by my friend Aaron Schaefer. So, thanks, Aaron!