Post to Twitter from a Linux Shell

I was having a very slow morning at work and I started experimenting with sending tweets from the command line in Linux. I realize this is a little silly and there are plenty of good twitter apps out there but I found it amusing so I decided to post it.


NOTE: You must have curl installed for this to work but I believe its standard on most Linux distros.

The first way to do it is just a one liner:

bash

  1. curl -u user:pass -d status="Here is my tweetl" http://twitter.com/statuses/update.xml

The only problem with the one liner is that it doesnt check to make sure your tweet is 140 chars or less and it also gives a bunch of xml output after it posts to the twitter API.

So I decided to whip up a quick shell script:

bash

  1. #!/bin/bash
  2. host=http://twitter.com/statuses/update.xml
  3. max=140
  4. echo "Please enter your twitter username:  "
  5. read user
  6. while [ "$user" == "" ];
  7. do
  8.                 echo
  9.                 echo "Please enter your twitter username:  "
  10.                 read user
  11.  
  12. done
  13.  
  14. echo "Please enter your password:  "
  15. read passwd
  16. while [ "$passwd" == "" ];
  17. do
  18.                 echo
  19.                 echo "Please enter your password:  "
  20.                 read passwd
  21.  
  22. done
  23.  
  24. echo "Enter a status update (140 chars max):  "
  25. read status
  26.  
  27. while [ "$status" == "" ];
  28. do
  29.                 echo
  30.                 echo "Enter a status update (140 chars max):  "
  31.                 read status
  32.  
  33. done
  34.  
  35. while [ ${#status} -gt  $max ];
  36. do
  37.                 echo
  38.                 echo "Your status is longer then 140 chars"
  39.                 echo -en "\nEnter a status update (140 chars max):  "
  40.                 read status
  41. done
  42. curl -u  $user:$passwd -d status="$status" $host  > /dev/null 2>&1

Just save the script and call it something like twitter_shell.sh and make it executable:

bash

  1. chmod 755 twitter_shell.sh

Thats it, now you can run the script when you are working on Linux servers and make twitter updates when you should be working!

The Twitter Book (Paperback)


List Price: $19.99
New From: $0.96 USD In Stock
Used from: $0.01 USD In Stock

Twitter Power 2.0: How to Dominate Your Market One Tweet at a Time (Paperback)


List Price: $16.95
New From: $0.50 USD In Stock
Used from: $0.01 USD In Stock

Share