#!/bin/bash
# pid-identifier
# http://www.tldp.org/LDP/abs/html/procref1.html
# Gives complete path name to process associated with pid.
ARGNO=1 # Number of arguments the script expects.
E_WRONGARGS=65
E_BADPID=66
E_NOSUCHPROCESS=67
E_NOPERMISSION=68
PROCFILE=exe

# Check correct number of arguments used
if [ $# -ne $ARGNO ]
then
	echo "Usage: `basename $0` PID-number" >&2 # Error message >stderr.
	exit $E_WRONGARGS
fi
# Check for valid process ID
pidno=$( ps ax | grep $1 | awk '{ print $1 }' | grep $1 )
if [ -z "$pidno" ] # zero-length string means no running process corresponds to the pid given
then
	echo "No such process running."
	exit $E_NOSUCHPROCESS
fi
# Check for read permission
if [ ! -r "/proc/$1/$PROCFILE" ]
then
	echo "Process $1 running, but..."
	echo "Can't get read permission on /proc/$1/$PROCFILE."
	exit $E_NOPERMISSION # Ordinary user can't access some files in /proc.
fi
# Find the executable exe_file=$( ls -l /proc/$1 | grep "exe" | awk '{ print $11 }' )
if [ -e "$exe_file" ]	# If /proc/pid-number/exe exists,
then			#+ then the corresponding process exists.
	echo "Process #$1 invoked by $exe_file."
else
	echo "No such process running."
fi
exit 0


syntax highlighted by Code2HTML, v. 0.9.1