Check out this plugin aciton:
Keyboard Maestro Plug-In – Terminal Execute Command
or this more in depth discussion / examples:
For more information, see the wiki Searching the Forum for Answers section.