Posts Tagged ‘ bash

Automapping channel numbers to TiVo commands from the Ubuntu command line

In a previous post, I described how to control your TiVo from the Linux command line. I wanted to take it a bit further and make it easier to enter numeric IR codes to my TiVo. With some pointers from Alex Lambert, I came up with a solution that works on my Ubuntu 12.04 laptop.

I am assuming you followed the instructions in the previous post and have created the following symlinks:

lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM0 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM1 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM2 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM3 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM4 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM5 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM6 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:24 NUM7 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:25 NUM8 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:25 NUM9 -> tivoircode
lrwxrwxrwx 1 chuck chuck   10 Dec  4 23:32 ENTER -> tivoircode

Now, the goal here is that simply entering the command such as “1695” will send the sequence NUM1;NUM6;NUM9;NUM5 to the TiVo. This can be accomplished using the same facility that Ubuntu uses to suggest packages to install when you issue a command from a package that is not currently installed. To do this, add the following to your ~/.bashrc:

function is_integer() {
    s=$(echo $1 | tr -d 0-9)
    if [ -z "$s" ]; then
        return 0
    else
        return 1
    fi
}

command_not_found_handle() {
        if is_integer $1; then
                for NUM in `echo $1 | fold -w1`
                do
                        NUM$NUM
                done
#               ENTER
        else
                if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
                if [ -x /usr/lib/command-not-found ]; then
                   /usr/bin/python /usr/lib/command-not-found -- "$1"
                   return $?
                elif [ -x /usr/share/command-not-found/command-not-found ]; then
                   /usr/bin/python /usr/share/command-not-found/command-not-found -- "$1"
                   return $?
                else
                   printf "%s: command not found\n" "$1" >&2
                   return 127
                fi
                fi

        fi
}

Now, when you enter an numeric integer value on your bash session, it will send it to your TiVo as a series of IR codes for each digit in the number. You can uncomment the final ENTER command if you wish for the enter button to be pressed when a number is finished being entered on the TiVo, but that is up to you. This function copies the body of the command_not_found_handle default function from /etc/bash.bashrc that ships with Ubuntu, so that non-integer commands are sent through the old mechanism and no functionality is lost.

Happy TiVo’ing