Shell Programming:
Batch Files, continued...
What happens when
the script is called? The if construct checks whether there is
a argument given to the script. If it is missing, it prompts
you a little help and terminates gracefully.
If there is an
argument, it takes it as the name or a part of the name of a
library, which will be stored in $1. "ldconfig -p"
reports ALL shared libraries, which are known by the system along
with the paths, where they are stored. This output, if piped
though grep, looks for all lines containing the given name of
that library. The "-i" switches grep into "ignore
case" mode.
This is more convenient
for all those X-related libraries, because they use also uppercase
letters in their names...
*************************************************
Newbie note: "X-related libraries are not dirty pictures.
They are function libraries for X-windows, which does for Unix
systems what Windows does for Unix. If you want point and
click hacking, install a Unix type operating system on your home
computer and get X-windows running.
*************************************************
The output of grep
(all lines containing the library name) are piped into gawk,
which filters all but the fourth argument (the path to the library
and the library name itself) out of the stream. Each line of
the finally resulting output is taken -- line by line -- by a for-loop.
For each looping $i contains one new line -- a path and the library
name -- which is take by "ls". There is a little trick
at this line.
There is not only
a
ls "$i"
There is also a
ls "$i"*
This is because
they may be different versions of that library, which can be
listed by using ls "$i"*.
Finally the output
of the loop is piped through "sort -u" which sorts
the output alphabetically and removes all identically lines.
Therefore each line is only reported once. The "-u"
switch stands for "unique".
Next one!
------------------------------------------------------------------
(store this script as "ldprint"
#!/bin/sh
#
# prints all libraries used by a
# certain program
#
############################################
if [ -z $1 ]
then
echo "usage: ldprint <program to examine>"
exit
fi
ldd $(which $1)
This one is simple.
It runs with the same versions of tools mentioned above. Additionally:
ldd is of version 1.9.6.. The version of "which" is
unknown to me, but this program is so "unspecial",
that I think, all versions should work.... :-)
What happens here?
ldd reports all libraries, which are used by a certain program.
But! if you type
ldd emacs
This will not work
in most cases, cause emacs is not in the current directory. "ldd"
will only work if the full path to the program (emacs) is specified.
If you type
which emacs
It will print (for
example)
/usr/X11R6/bin/emacs
Now! If you type
ldd which emacs
The shell cannot
decide correctly what to call first: ldd, which, or emacs. It
decides: First is the tool, all further things are arguments
to ldd.
Wrong!!!
This is why we
have to give the shell a hint, a tip. Are we friendly hackers?
YES! OK,
The $(which $1)
replace "which $1" with the result of the run of which,
removes the $( and the closing ) and THEN it will execute ldd.
Now ldd "sees" the full path, the call of which left
behind.
But this is a more
complicate thing. It is called "argument substitution"
and you should read more about it in the man pages of your shell.
More on shell batch files --->>