When I enter a command, did I get the correct instance?

Are you sometimes puzzled or surprised by an instance of executable being run when you did not expect it to?

Here is a script to clarify

  • which instances exist
  • why that instance was the one that was run

Hope you find the script useful.

:slight_smile:


Sample output:

Found 3 instances of the command ...

Instance				Path Precedence
----------------------------------------------------------------------
	--				/Oasis/bin
	--				/root/.nvm/versions/node/v22.4.1/bin
	--				/DB001_F2/Oasis/bin
	--				/DB001_F2/Oasis/bin_Others
	--				/DB001_F2/LO/bin_Util
	--				/DB001_F2/LO/bin_Admin
	--				/DB001_F2/LO/bin_OS
	--				/DB001_F2/LO/bin_FW
	--				/usr/local/sbin
/usr/local/bin/firefox (symlink)         🡺   /opt/firefox/firefox
	--				/usr/sbin
	--				/usr/bin
	--				/sbin
	--				/bin
	--				/usr/games
	--				/usr/local/games
	--				/snap/bin
	--				/DB001_F2/LO/bin_User
	--				/DB001_F2/LO/bin_Eval
	--				/DB001_F2/LO/bin_Dev

Masked Instances
----------------------------------------------------------------------
-rwxr-xr-x   /DB001_F2/home/ericthered.Downloads/firefox/firefox
-rwxr-xr-x   /opt/firefox-142.0/firefox

Script [ Appl__Command__PathAndPrecedence.sh ]:

#!/bin/sh

tmp="$(basename "${0}" ".sh")_$$.tmp"

if [ $# -eq 0 ]
then
	echo "\n\t Required name of executable attempting to locate as parameter.\n" ; exit 1
fi


reportInstance()
{
	if [ -L "${testor}" ]
	then
		target="$(ls -l "${testor}" | cut -f2- -d\> | cut -c2- )"
		if [ -x "${target}" ]
		then
			echo "${testor} (symlink)" |
			awk -v tgt="${target}" '{ printf("%s%s%-40s%s 🡺  %s%s%s%s\n", "\033[1m", "\033[33;1m", $0, "\033[0m", "\033[1m", "\033[92;1m", tgt, "\033[0m" ) ; }'
		else
			if [ -s "${target}" ]
			then
				echo "${testor} (noexec)" |
				awk -v tgt="${target}" '{ printf("%s%s%-40s%s 🡺  %s%s%s%s\n", "\033[1m", "\033[33;1m", $0, "\033[0m", "\033[1m", "\033[91;1m", tgt, "\033[0m" ) ; }'
			else
				echo "${testor} (broken)" |
				awk -v tgt="${target}" '{ printf("%s%s%-40s%s 🡺  %s%s%s%s\n", "\033[1m", "\033[33;1m", $0, "\033[0m", "\033[1m", "\033[91;1m", tgt, "\033[0m" ) ; }'
			fi
		fi
	else
		if [ -x "${testor}" ]
		then
			echo "${testor}" | awk '{ printf("%-40s 🡸\n", $0 ) ; }'
		else
			echo "\t--\t\t\t\t${dir}"
		fi
	fi
}


cmd=${1}
locate "${cmd}" |
	awk -v fcmd="${cmd}" '{
		n=split( $0, parts, "/" ) ;
		if( parts[n] == fcmd ){
			print $0 ;
		} ;
	}' |
	while true
	do
		read line
		if [ -z "${line}" ]
		then
			break
		else
			if [ ! -d "${line}" ] && [ -x "${line}" ]
			then
				echo "${line}"
			fi
		fi
	done >"${tmp}"

if [ -s "${tmp}" ]
then
	echo "\nFound $(wc -l "${tmp}" | awk '{ print $1 }' ) instances of the command ..."
else
	echo "\nFound  0  instances of the command.\n" ; exit 0
fi

cpath=$(which "${cmd}" )
locn=$(dirname "${cpath}" )

echo "\nInstance\t\t\t\tPath Precedence\n----------------------------------------------------------------------"

cmdMatch=0
for dir in $(echo "${PATH}" | sed 's+[:]+\ +g' )
do
	testor="$(grep '^'"${dir}" "${tmp}" )"
	if [ -n "${testor}" ]
	then
		cmdMatch=1
		reportInstance
	else
		if [ ${cmdMatch} -eq 1 ]
		then
			testor="${dir}/${cmd}"
			reportInstance
		else
			echo "\t--\t\t\t\t${dir}"
		fi
	fi
done

grep -v "${cpath}" "${tmp}" >"${tmp}.2"
if [ -s "${tmp}.2" ]
then
	echo "\nInstances Masked from PATH\n----------------------------------------------------------------------"
	cat "${tmp}.2" | 
	xargs ls -l |
	awk '{
		pos=index( $0, $9 ) ;
		rem=substr( $0, pos ) ;
		printf("%-12s %s\n", $1, rem ) ;
	}'
	echo ""
else
	echo "\nFound no instances of the command which are Masked from PATH.\n"
fi
2 Likes