Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script to show compatible commands based on "Windows-speak"

Tags:

Problem: Customer X is a Windows user who wants to be able to trigger pre-packaged bash commands by using mnemonic keywords or "tag hints" when she is logged in to her RedHat box via shell.

Example: Customer X logs into host using ssh and wants to do some routine file operations. She wants to be able to type

copy file

and get back a listing of pre-fab fill-in-the-blank bash commands to choose from

cp <@source@> <@dest@>               ### simple copy
cp -R <@startdir@>  <@destdir@>      ### recursive copy

she then wants to be able to select one of these items, fill in the blank(s) and just hit enter to run the command.

Customer X is willing to specify ahead of time what commands she is likely to want to use (in windows-speak) and then hire the developer to translate those into bash commands, and then put them together in a script that allows her to talk windows-speak to bash and get back the list of commands.

NOTE: Customer X doesn't like apropos because it assumes familiarity with terms used in bash, as opposed to windows-speak. For example:

 apropos shortcut

doesn't give her anything about creating symlinks (even though that is exactly what she wants) because she doesn't know what windows shortcuts are called in linux. Obviously, windows concepts don't carry over 100% so she will have to learn eventually, but she's a busy person and is asking for this as a way to "ease" her into linux understanding.

Question: What is the best way to get started on something like this? Is there a perl, python, ruby script out there that does something like this already? Is there something in bash that can simulate this kind of feature request?

like image 506
dreftymac Avatar asked Dec 19 '08 01:12

dreftymac


1 Answers

What you probably want is to override bash's command-not-found handler. Here's the section in /etc/bash.bashrc in a standard Ubuntu install that installs the handler:

...
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ]; then
        function command_not_found_handle {
                # check because c-n-f could've been removed in the meantime
                if [ -x /usr/lib/command-not-found ]; then
                   /usr/bin/python /usr/lib/command-not-found -- $1
                   return $?
                else
                   return 127
                fi
        }
fi
...

In effect, if a command is not found, a user specified program is executed with that command as a parameter. In the case of Ubuntu, it's a Python program that checks to see if the command the user typed is a valid application that can be installed, and if it is, informs the user that he/she can install it.

What you probably want to do is compare it to you hashref of commands and usage strings and display the appropriate one if there's a match.

like image 64
codelogic Avatar answered Sep 30 '22 17:09

codelogic