Take your bash_aliases with you

4 Jun

I love aliases. There are many useful shortcuts, e.g. „..“ as an alias for „cd ..“. If I got shell access to a system, I first install my version of vimrc, bashrc, bash_alias, and so on. I keep those files in a subversion repository, so I can synchronize them.

But what to do, when aliases are „system-sensitive“?

Some commands only work when required programs are installed or if a special directory exists. Most of the times, this is not a problem: if the program is not installed, the alias simply does not work. But what, if you want to install „colormake“ as „make“. Okay, this seems to be a bad idea, but I think, you understand the point .

A workaround would be to test weather a desired program is installed before installing an alias. This worked nicely. Until I got access to a Router and a very slow PC: the login needed 5 seconds. I decided to write a simple DSL, which creates my bash_alias based on a series of checks. The resulting file is than static. Here is the first part of the script:

The file opens itself, seeks „###END###“ and processes everything after it. It defines several commands:

  • ::e:myfile – skip lines until next ::c if myfile does not exist
  • ::d:mydir – skip lines until next ::c if directory mydir does not exist
  • ::r:myfile – skip lines until next ::c if file myfile is not readable
  • ::n – inverts skipping state (imagine this as „else)
  • ::p:module – skip lines until next ::c if the perl module module is not installed
  • ::c – clear skip state

This is freaky to read, but simple to use and it can be easily extended. Note the explicit call of /bin/echo in line 97. This was the only way I found to get this script working witch both dash and bash (their „echo“ words differently if they find a backslash).

Okay, last but not least some examples for the second part (most parts are collected from several websites):

To create a customized bash_aliases just run shomething like „sh mkalias.sh > ~/.bash_aliases„.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.