WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Autoloading extensions with Bash  (Read 2211 times)

Offline althalus

  • Sr. Member
  • ****
  • Posts: 351
Autoloading extensions with Bash
« on: March 24, 2010, 06:51:22 PM »
Here's an interesting little curiosity for anyone who uses bash instead of sh. Put it in your .bashrc for a somewhat automatic "ondemand" when launching apps from command line.

If the command you type isn't found, bash will fall back onto a function called command_not_found_handle() if it doesn't exist.

This is an example of a command_not_found_handle() that will look in your tcz directory for an extension with the same name as the failed command. If it finds a match, the extension will be loaded, and your command re-run.

Failing that, it will grep the *.tcz.list files (if you have them) and find a match that way. Again, if it finds a match, load the extension and re-execute your command. If grep finds more than one extension matching it's search, proceed at your own risk. Have not tested nor planned for this eventuality.

It took me a total of 20 minutes to write and test, so not guaranteed to work, though it does the job well enough for me.

Code: [Select]
function command_not_found_handle()
{
    TCE_DIR=`cat /opt/.tce_dir`
    [ -f ${TCE_DIR}/optional/${1}.tcz ] && (
        tce-load -i ${TCE_DIR}/optional/${1}.tcz
        (which $1) && $@
        exit
    )
    TCZ=`grep "${1}$" ${TCE_DIR}/optional/*.tcz.list`
    [ "${TCZ}" == ""] || (
        tce-load -i ${TCZ%.list}
        $@
        exit
    )
}
« Last Edit: March 24, 2010, 09:41:30 PM by althalus »

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: Autoloading extensions with Bash
« Reply #1 on: March 24, 2010, 08:16:35 PM »
For those not using '/mnt/hda1/tce' it should work "automagically" if one replaces /mnt/hda1/tce with $(cat /opt/.tce_dir)


Offline althalus

  • Sr. Member
  • ****
  • Posts: 351
Re: Autoloading extensions with Bash
« Reply #2 on: March 24, 2010, 09:42:06 PM »
Updated the script slightly with maro's advise in mind.