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:
- Will this produce a reliable result across platform?
- Is there a slicker way to parse the output开发者_StackOverflow of ldconfig?
Thanks, Paul
There're several ways the library is loaded by executeable: 1.
- Using $LD_LIBRARY_PATH
- Using ld cache
- Libary with full path compiled into binary (-rpath gcc flag)
You're using option 2, while option 1 and 3 are not considered.
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
.
精彩评论