LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Blogs > konsolebox
User Name
Password

Notices

Rate this Entry

libfind - Searches library files with keywords and paths from default values and /etc/ld.so.conf.

Posted 09-17-2012 at 01:03 AM by konsolebox
Updated 09-17-2012 at 06:23 AM by konsolebox
Tags bash, script

I made this utility along with binfind. It also has had lots of improvements since then.

This script can be used to search files contained in library files (*.so, *.la, *.a, etc.) based from keywords provided. Along with a custom list of default paths, it will search for libraries inside directories that are specified in /etc/ld.so.conf. It would also examine files that are specified by the "include" directive in it.

After preparations it would search for the files that match all the provided keywords and show them to the user in plain format.

Usage: /usr/local/bin/libfind <partstring> [partstring2, ...]

I favor placing those scripts in /usr/local/bin. That usage instruction there is an output of the script when no argument is passed unto it.

Code:
#!/bin/bash

# Check bash version.

[[ BASH_VERSINFO -ge 3 ]] || {
	echo "$0: You need bash version 3.0 or higher to run this script." >&2
	exit 1
}

# LIBPATH Variable. You could add or remove default path values here.

LIBPATHS=('/lib' '/usr/lib' '/usr/local/lib')

# Functions

function getabspath {
	local -a T1 T2=()
	local -i I=0
	local IFS=/ A

	if [[ $1 == /* ]]; then
		read -a T1 <<< "$1"
	else
		read -a T1 <<< "$PWD/$1"
	fi

	for A in "${T1[@]}"; do
		case "$A" in
		..)
			[[ I -gt 0 ]] && unset T2\[--I\]
			continue
			;;
		.|'')
			continue
			;;
		esac

		T2[I++]=$A
	done

	if [[ $1 == */ ]]; then
		if [[ I -ne 0 ]]; then
			__="/${T2[*]}/"
		else
			__=/
		fi
	elif [[ I -ne 0 ]]; then
		__="/${T2[*]}"
	else
		__=/.
	fi
}

function getlibpaths {
	local FILE=$1 LINE

	[[ -f $FILE && -r $FILE ]] || return

	while read LINE; do
		case "$LINE" in
		/*)
			LIBPATHS[${#LIBPATHS[@]}]=$LINE
			;;
		include\ *)
			local PATTERN=${LINE##include+([[:blank:]])}

			if [[ $PATTERN != /* ]]; then
				getabspath "$FILE"
				PATTERN=${__%/*}/${PATTERN}
			fi

			while read LINE; do
				getlibpaths "$LINE"
			done < <(compgen -G "$PATTERN")
			;;
		esac
	done < "$FILE"
}

function main {
	# Set extended globbing.

	shopt -s extglob

	# Check arguments.

	[[ $# -eq 0 || "$1" == "-h" || "$1" == "--help" ]] && {
		echo "Usage: $0 <partstring> [partstring2, ...]"
		return 1
	}

	# Prepare patterns.

	local A
	local -a IPATTERNS=(-iname "*$1*")

	for A in "${@:2}"; do
		IPATTERNS=("${IPATTERNS[@]}" -and -iname "*$A*")
	done

	# Prepare paths.

	getlibpaths "/etc/ld.so.conf"

	# Make list unique.

	local -a T=("${!LIBPATHS[@]}")
	local -i I=0 J C=${#T[@]} D=0
	for (( ; I < C; ++I )); do
		for (( J = I + 1; J < C; ++J )); do
			[[ ${LIBPATHS[T[I]]} = "${LIBPATHS[T[J]]}" ]] && {
				unset LIBPATHS\[T\[J\]\] T\[J\]
				(( ++D ))
			}
		done
		[[ D -gt 0 ]] && {
			T=("${T[@]:I + 1}")
			(( C -= D + I + 1, I = -1, D = 0 ))
		}
	done

	# Remove unusable entries.

	for I in "${!LIBPATHS[@]}"; do
		[[ "${LIBPATHS[I]}" == @(|+([[:blank:]])) ]] && unset 'LIBPATHS[I]'
	done

	# Remove directories that do not exist, are not readable, or is not executable.

	for I in "${!LIBPATHS[@]}"; do
		[[ -d "${LIBPATHS[I]}" && -r "${LIBPATHS[I]}" && -x "${LIBPATHS[I]}" ]] || unset 'LIBPATHS[I]'
	done

	# Find.

	[[ ${#LIBPATHS[@]} -gt 0 ]] && find "${LIBPATHS[@]}" -maxdepth 1 -xtype f "${IPATTERNS[@]}" 2>/dev/null
}

# Start

main "$@"
Example output:
Code:
# libfind kde games
/usr/lib64/libkdegames.so
/usr/lib64/libkdegames.so.5
/usr/lib64/libkdegames.so.5.1.0
Note: It's not necessary that keywords are not intersective.
Posted in Howtos
Views 715 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 08:55 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration