BASH script: Generate PNG image with text from CLI

A nice little BASH script which generates a PNG image (black on white by default) from a text source (eighter from a pipe or stdin until you press CTRL+D).
if [ "$1" = "--help" ] ; then
echo -e "Usage:\
Pipe some text into $0 with filename as an optional parameter."
exit 1
if [ -n "$1" ] ; then
file="cli-"`date +%s`".png"
echo "Saving to: $file"
cat | convert label:@- $file

Once again inspired by 

BASH script: Save unsynced data to disk

Displays amount of data waiting to be written to disk(s) and invokes sync(1) to force changed blocks to disk and update the super block.
saved=`grep ^Dirty /proc/meminfo | sed 's/Dirty\:[[:space:]]*//g`
echo "Syncing $size of unsaved data to disk(s)."

Example output:
sairon@arch-sd:~$ dirty
Syncing 84 kB of unsaved data to disk(s). 

Thanks to:

My shell scripts #5 - Google dictionary API (word definitions)

This two-file google dictionary API, which returns a full definition of a given word in all its forms (noun, adjective, verb, abbreviation, ...), along with a direct link to an mp3 file containing the word spoken by a google text-to-speech api (and a lot of other useful informations).
This script is divided into two separate scripts (shell and PHP) only because it is quite hard to parse JSON strings with line-oriented text processors (sed, grep, get the idea). First, the shell script gets the actual data from google, does some filtering and substitution to make it easier for the following PHP script, which then iterates through the parsed JSON string and outputs the data you want (and also adds some colour).
Don't forget to change the names and paths accordingly to suit your environment and needs.

The shell script:
if [ "$1" = "-" ]
  read word
  word=`echo $word | sed 's/\ /\+/g'`
if [ ! -n "$word" ]
  echo Specify a word to lookup in glossary.
  echo Usage:
  echo "  dict word"
  echo "use - (dash) if reading word from stdin"
  exit 1
  wget -qO - "$word&sl=$lang&tl=$lang&restrict=pr,de&client=te" \
  | sed 's/dict_api\.callbacks.id100.//' \
  | sed 's/\,\"webDefinitions.*//' \
  | sed 's/\\x3cem\\x3e//g' \
  | sed 's/\\x3c\/em\\x3e//g' \
  | sed "s/\\\x27/\'/g" \
  | php-cli /opt/scripts/gloss-json.php
exit 0
And the PHP one:
    $fd = fopen("php://stdin", "r");
    $json = "";
    while (!feof($fd)) {
        $json .= fread($fd, 1024);
    $json = $json.'}';
    $json = json_decode($json);
    $word = $json->query;
    $primaries = count($json->primaries);
    if ($primaries == 0) echo "Sorry, no match.\n";
    for ($i = 0; $i < $primaries; $i++) {
        echo "\033[1;36m" , $json->primaries[$i]->terms[0]->text , " | ", $json->primaries[$i]->terms[0]->labels[0]->text ,
             "  ";
        if ($json->primaries[$i]->terms[1]->type == "phonetic") echo $json->primaries[$i]->terms[1]->text;
        echo "\033[0m\n";
        $cnt = 1;
        for ($j = 0; $j < count($json->primaries[$i]->entries); $j++) {
            if ($json->primaries[$i]->entries[$j]->type == "related") {
                for ($l = 0; $l < count($json->primaries[$i]->entries[$j]->terms); $l++) {
                    echo "\033[0;36m" , $json->primaries[$i]->entries[$j]->terms[$l]->text , "\033[0m ";
                    echo $json->primaries[$i]->entries[$j]->terms[$l]->labels[0]->text , "; ";
                echo "\n";
            if ($json->primaries[$i]->entries[$j]->type == "meaning") {
                echo "  " , $cnt , ") " , $json->primaries[$i]->entries[$j]->terms[0]->text , "\n";
                for ($k = 0; $k < count($json->primaries[$i]->entries[$j]->entries); $k++) {
                    echo "    * " , $json->primaries[$i]->entries[$j]->entries[$k]->terms[0]->text , "\n";

My shell scripts #4 - search API

NOTE: This script is now deprecated. Continue to the new version.

A command-line interface for! Usage is simple:
./ [search string]
You can easily change transmission to any other torrent client (which supports adding torrents by URL via CLI). The dirty sed magic can be optimized, but it works as is, so why bother? ;) Enjoy! script in action via PuTTY
The code:
search() {
  q=`echo $q | tr ' ' '+'`
  results=`wget -U Mozilla -qO - "$q/0/7/0" \
    | egrep -o '(http\:\/\/.*\.torrent)|(Size\ .*B\,)|([[:digit:]]*)' \
    | sed 's///g' \
    | sed 's/<\/td>//g' \
    | sed 's/Size\ //g' \
    | sed 's/\&nbsp\;//g' \
    | sed 's/\,//g' \
    | sed 's!!!g' \
    | sed ':a;N;$!ba;s/\n/\ /g' \
    | sed 's!\ \/!\n\/!g' \
    | sed 's/MiB\ /M\ /g' \
    | sed 's/GiB\ /G\ /g' \
    | sed 's/KiB\ /K\ /g' \
    | cat`
  for line in $results
    length=`echo "$line" | awk '{print $1}' | wc -L`
    if [ $length -gt $longest ]
  for line in $results
    length=`echo $line | awk '{print $1}' | wc -L`
    nth=`echo "$nth + 1" | bc -l`
    spaces=`echo "$longest + 1 - $length" | bc -l`
    for i in `seq 1 $spaces`
      whites=`echo "$whites "`
    torrent=`echo $line | awk '{print $1}' | sed 's!\/[[:digit:]]*\/!!' | sed 's!\.[[:digit:]]*\.TPB\.torrent!!' | sed 's/_/\ /g'`
    size=`echo $line | awk '{print $2}'`
    size=`printf '%10s' "$size"`
    seeds=`echo $line | awk '{print $3}'`
    seeds=`printf '%5s' "$seeds"`
    peers=`echo $line | awk '{print $4}'`
    peers=`printf '%5s' "$peers"`
    if [ $nth -lt 10 ]
      nth=" $nth"
    echo -e "$nth  $torrent$whites\033[1;34m$size\t\033[1;31m$seeds\t\033[1;32m$peers\033[0m"
download() {
  echo -n "Download? "
  read num
  if [ $num -eq 0 ]
    exit 1
    for line in $results
      i=`echo "$i + 1" | bc -l`
      if [ $i -eq $num ]
        link=`echo "$line" | awk '{print $1}'`
        echo "Downloading torrent file."
        transmission-remote -a "$link"
        echo "Torrent added."
        exit 0
search $*
unset IFS
exit 0

My shell scripts #3 - Google Text-to-Speech API

This simple script outputs an mp3 audio of a given text string ready to be played by some commandline app (sox, madplay, ...). The only thing I can't figure out is how to get it to accept some national characters (right now it just says some gibberish). If you manage to figure out this one problem, I'd be more than happy to hear from you in the comments ;).

$ say en lol #says "lol" in english
$ somecommand | say fr - #read text to say from a pipe
And here goes the script:
if [ "$1" = "-" ]
  read text
  echo $text
len=`expr length "$text"`
if [ -z "$text" ] ; then
        echo "Please specify string to translate (up to 100 characters incl.)."
        exit 4
elif [ "$len" -gt "100" ] ; then
        echo "Can't translate more than 100 characters at once! (entered $len)"
        exit 2
wget -qU Mozilla -O - "$lang&q=$text" | madplay -Q -o wave:- - | aplay -q -
exit 0

My shell scripts #2 - WHOIS search

This time, we're going to get some informations about domains through WHOIS servers. All this via CLI interface and without the ubiquitous "whois" command (which is not available for my CPU architecture in OpenWrt package repository and I'm too lazy to cross-compile it myself). Used programs: "netcat", "sed", "grep".

The server automatically redirects you to the WHOIS server responsible for the supplied TLD, so you don't have to remember them (and it also simplifies this script).
if [ ! -n "$1" ]
  echo "Usage:"
  echo "  $0 domain.tld"
  exit 1
tld=`echo $1 | sed 's/\/.*//' | grep -o '[[:alpha:]]*$'`
echo "$1" | netcat -T $ 43
exit 0

My shell scripts #1 - Wikipedia search

I will publish my creations here, just in case someone finds them useful. My scripts are made for the ash shell, so they should work in most of the other shells. I run them on my OpenWrt router with 400MHz CPU and 32MB RAM with no problems :).

The first one I'm going to publish is a Wikipedia search script, requiring only "dig" and "sed" programs. Usage is fairly simple, just type:

$ ./ hello world

The script automatically replaces all spaces with underscores. And now the actual code:

if [ ! -n "$1" ]
  echo "What are you searching for?"
  echo "Usage:"
  echo "  $0 keyword"
  exit 1
q=`echo $* | sed 's/\ /\_/g'`
dig +short txt $ | sed 's/\"\ \"//'
exit 0