views:

47

answers:

2

Hello,

I need to find out which library will be loaded given in the information returned from /sbin/ldconfig. I came up with the following:

#!/bin/bash
echo $(dirname $(/sbin/ldconfig -p | awk "/$1/ {print \$4}" | head -n 1))

Running this results with:

$ whichlib libGL.so
/usr/X11R6/lib

This a two part question:

  1. Will this produce a reliable result across platform?
  2. Is there a slicker way to parse the output of ldconfig?

Thanks, Paul

A: 

Depending on what exactly you're doing you may want to run ldd directly on the executable you're planning to run rather than the general case ldconfig.

Since you asked, you could write your script like this:

dirname "$(/sbin/ldconfig -p | awk "\$1 == "$1" {print \$4; exit}")"

It's a little more precise and has one less pipe. Also echo $(cmd) is redundant; you can just write cmd.

John Kugelman
Your version doesn't accept a command-line argument. In the OP's, `$1` is a Bash argument and `\$4` is an awk field. In yours, `$1` and `$4` are both awk fields and the lib being searched for is hard-coded.
Dennis Williamson
Oops, you're right. I hard coded it when testing on the command-line.
John Kugelman
Just a reminder, don't use ldd on untrusted code: http://www.catonmat.net/blog/ldd-arbitrary-code-execution/
profjim
A: 

There're several ways the library is loaded by executeable: 1.

  1. Using $LD_LIBRARY_PATH
  2. Using ld cache
  3. Libary with full path compiled into binary (-rpath gcc flag)

You're using option 2, while option 1 and 3 are not considered.

dimba