LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > konsolebox
User Name
Password

Notices


Rate this Entry

binfind - Search for executable files based from PATH variable.

Posted 09-04-2012 at 09:51 PM by konsolebox
Updated 05-27-2018 at 03:11 PM by konsolebox
Tags bash, script

I've been using this utility quite long enough already. I use this to search for executable files based from the path values saved in the PATH variable.

Code:
#!/bin/bash

shopt -s extglob

function main {
	# check bash version

	[[ BASH_VERSINFO -ge 3 ]] || {
		echo "This script requires bash version 3.0 or newer."
		return 1
	}

	# check arguments

	[[ $# -eq 0 || "$1" == "-h" || "$1" == "--help" ]] && {
		echo "usage: binfind <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

	local IFS=:
	read -r -a PATHS <<< "$PATH"

	# make list unique

	local -a T=("${!PATHS[@]}")
	local -i I=0 J C=${#T[@]} D=0
	for (( ; I < C; ++I )); do
		for (( J = I + 1; J < C; ++J )); do
			[[ ${PATHS[T[I]]} = "${PATHS[T[J]]}" ]] && {
				unset PATHS\[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 "${!PATHS[@]}"; do
		[[ "${PATHS[I]}" == @(|+([[:blank:]])) ]] && unset 'PATHS[I]'
	done

	# find

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

main "$@"
Get the updated version of this script here.
Posted in Howtos
Views 1659 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 04:04 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration