There are numerous desktop graphical Twitter clients for both GNOME and KDE. There is also a SourceForge project created in 2008 to create a command line Twitter client that does not have any project files uploaded for it and some scripts around the Net in various stages of completeness. I wanted one I could use to automate tweets from my desktop.
I wanted the ability to post to multiple accounts, but without having to enter both username and password every time. I also did not want to have to configure it if I didn’t want to. So, I created one that uses an rc file to store username:password combinations for multiple accounts. The first account listed is used by default if none is specified. Alternatively, just the username may be specified. The script will get the password from the rc file. And accounts that are not in the resource file can be used by specifying both username and password.
Here is the script:
#!/bin/bash -
#===============================================================================
#
# FILE: tw
#
# USAGE: tw -h | [-u USERNAME] [-p PASSWORD] "tweet"
#
# DESCRIPTION: Command line client to post tweets to Twitter
#
# OPTIONS: -u USERNAME -p PASSWORD -h
# REQUIREMENTS: curl
# BUGS: ---
# NOTES: Multiple accounts can be stored in ~/.twrc as username:password
# pairs one per line
# AUTHOR: Scott Bicknell
# COMPANY:
# VERSION: 1.0
# CREATED: 07/03/2009 01:09:02 PM PDT
# REVISION: ---
#===============================================================================
declare -r MAX_LENGTH=140 # maximum tweet length
declare -r rc=${HOME}/.${0##*/}rc # configuration file
usage() {
cat >&2 <<EOF
Usage: ${0##*/} -h | [-u USERNAME] [-p PASSWORD] "tweet"
-u USERNAME is your twitter username. If no username
is supplied, the first username listed in the
configuration file is used.
-p PASSWORD is your twitter password. If no password
is supplied, it is read from the configuration
file.
"tweet" is the text of the Twitter update.
You can configure multiple twitter accounts using
the ${rc##*/} configuration file in your home
directory. The syntax for accounts is:
username:password
username:password
...
You can also use accounts without the configuration
file by supplying a username and password with the
-u and -p arguments.
EOF
}
cleanup() {
exit $1
}
error() {
printf "%s: %s " ${0##*/} "$@"
} >&2
while getopts :hu:p: opt; do
case "$opt" in
h) usage
cleanup 1
;;
u) username=$OPTARG
;;
p) password=$OPTARG
;;
*) error “unrecognized option: $OPTARG. Terminating…”
usage
cleanup 1
;;
esac
done
shift $(( $OPTIND - 1 ))
if ! which curl >/dev/null 2>&1; then
error "curl is required to use this program"
cleanup 1
fi
if [[ $# -eq 0 [[; then
error "No tweet supplied"
usage
cleanup 1
fi
if [[ -z $username [[ && ! [[ -f $rc [[; then
error "No twitter accounts have been configured"
usage
cleanup 1
fi
if [[ -z $username [[; then
account=$(head -n 1 $rc)
username=$(awk -F: '{print $1}' <<< $account)
password=$(awk -F: '{print $2}' <<< $account)
unset account
fi
if [[ -z $username [[; then
error "Username is missing from ${rc##*/} configuration file."
cleanup 1
fi
if [[ -z $password [[ && ! [[ -f $rc [[; then
error "No twitter accounts have been configured"
usage
cleanup 1
fi
if [[ -z $password [[; then
password=$(awk -F: '/'$username'/ {print $2}' $rc)
fi
if [[ -z $password [[; then
error "Password not configured for $username in ${rc##*/} configuration file"
cleanup 1
fi
if [[ $(echo $@ | wc -c) -gt $MAX_LENGTH [[; then
error "Tweets must be no longer than $MAX_LENGTH characters"
cleanup 1
fi
curl --basic --user "$username:$password" --data-ascii "status=$(echo $@|tr ' ' '+')" "http://twitter.com/statuses/update.json" -o /dev/null 2>/dev/null &&
echo Status updated ||
error "Unable to update Twitter status"
cleanup 0
This can be used from scripts and can be modified to take input from standard input, if desired. Enjoy.