A Shell script to search for a command pattern

Today I have been looking for a command to compare two files simultaneously (the colour schemes of KDE) and used diff. But that did not suit my requirement of viewing them simultaneously. There are editors like vi which provide simultaneous viewing of the files and I tried

$vimdiff file_1 file_2

To search for some specific command, we can use ‘man’ and the pattern

$man -k diff

and it showed many commands like zdiff,vimdif,..

The advantage of this is that it searches for the pattern and a one line abstract of these commands (available in man pages) for the corresponding pattern

But the major disadvantage is that if you have some commands in your system which does not have a man page, it returns “nothing appropriate”

Here is a simple shell script which searches for command-pattern in the directories in the PATH variable

Just make a change in the PATH_DIR_COUNT depending on the number of directories in it.
(use echo $PATH to find this out)

#!/bin/sh
# A simple Command to search for a pattern in the commands available in the directories mentioned in PATH environment varible
# Usage: cmd_search pattern

PATH_DIR_COUNT=6 #Number of Directories in the PATH environment variable

if test $# -lt 1
then
echo 'Usage: cmd_search pattern'
exit
fi

for i in `seq 1 $PATH_DIR_COUNT`
do
    ls `echo $PATH | cut -d ":" -f $i`|grep $1
done

To use the shell script, save it as cmd_search and

$chmod u+x cmd_search
$ ./cmd_search command_pattern

For example

$./cmd_search diff

Output

bzdiff
combinediff
dehtmldiff
diff
diff3
diff-jars
diffpp
diffstat
editdiff
espdiff
filterdiff
fixcvsdiff
flipdiff
gendiff
grepdiff
hdifftopam
interdiff
lsdiff
pamtohdiff
pdiff
rcsdiff
recountdiff
rediff
sdiff
sgmldiff
splitdiff
tiffdiff
unwrapdiff
vimdiff
zdiff
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s