rap reports which alias, function, script or executble, and contents

Hello.

I have installed 3-d party utilities, like gnu utilites,and adjusted the path. In order to make something work for someone else, I have to figure out which utility I use when I test. Adjusting away from /opt/local/bin, and /usr/local/bin when I have to.

I also type cat which utilitya lot to figure out what scripts look like. But I also type alias to see my aliases in .bashrc, and I use the builtin type function in bash to display the contents of functions (help type).

The shell function below, which should be included in your .bashrc file, or sourced into it. Solves this problem, as it will display any alias with a name, any function, the contents of a shell script, and the type of any binaries, in the executable order which is alias, function, and what comes first in the path.

[code]function rap(){
if [ $# -ne 1 ] ; then
echo “rap: Needs a command name!” >|/dev/stderr
return 1
fi
type -t “$1” 2>&1 |grep “function” >/dev/null && type “$1” && return 0

type -a "$1" 2>&1 | sed -ne '/aliased/p'
type -a "$1" 2>&1 | sed -ne '/shell builtin/p'
type -t "$1" 2>&1 |egrep "(aliased|builtin)" 2>&1 >/dev/null && return  0

rapTMPFILE=$(mktemp -q /tmp/rap.XXXXXX)
if [ $? -ne 0 ]; then
	echo "rap: Can't create temp file, exiting." >2
	exit 1
fi
type -a "$1" |sed -ne '/'"$1"' is \// s//\//p' >|$rapTMPFILE
exec 3<&0
exec 0<$rapTMPFILE
while read line
do
	# use $line variable to process line
	fileTp=$(/usr/bin/file $line)
	echo $fileTp
	echo $fileTp | grep -q "script text executable"
	if [ $? -eq 0 ] ; then 
		cat $line
	fi
done
exec 0<&3 function rap(){
if [ $# -ne 1 ] ; then 
	echo "rap: Needs a command name!" >|/dev/stderr
	return 1
fi
type -t "$1" 2>&1 |grep "function" >/dev/null && type "$1"  &&  return 0

type -a "$1" 2>&1 | sed -ne '/aliased/p'
type -a "$1" 2>&1 | sed -ne '/shell builtin/p'
type -t "$1" 2>&1 |egrep "(aliased|builtin)" 2>&1 >/dev/null && return  0

rapTMPFILE=$(mktemp -q /tmp/rap.XXXXXX)
if [ $? -ne 0 ]; then
	echo "rap: Can't create temp file, exiting." >2
	exit 1
fi
type -a "$1" |sed -ne '/'"$1"' is \// s//\//p' >|$rapTMPFILE
exec 3<&0
exec 0<$rapTMPFILE
while read line
do
	# use $line variable to process line
	fileTp=$(/usr/bin/file $line)
	echo $fileTp
	echo $fileTp | grep -q "script text executable"
	if [ $? -eq 0 ] ; then 
		cat $line
	fi
done
exec 0<&3 

}[/code]

Hello.

While I am at it; it is easy to see all aliases, just type alias in your terminal window, not so easy to see all the functions, so I have made two functions for displaying functions, functions, that removes most of the internal functions that goes with command completion, and one that shows everything: allfunctions

function functions() { set |/usr/bin/sed -n '/^\([^(][^(]*\)\([ ]\)\(()\)/ s//\1\3/p' |egrep -v "(COMPREPLY|^_|BASH|DIRSTACK|GROUPS)" }

function allfunctions() { set |sed -n '/^\([^(][^(]*\)\([ ]\)\(()\)/ s//\1\3/p' |grep -v "COMPREPLY" }
Source them, or paste them into your .bashrc. Use type “function-name” or rap “function-name” (as defined above) to display the contents of a function.

Rap is debugged, and upgraded, to work more correctly, when giving itself as input, and when getting the type of builtins.