3 # shprof - a line profiler for shell scripts
5 # adapted from a similar program included in `The New KornShell' by
6 # Bolsky and Korn and posted to usenet by bsh20858@challenger.fhda.edu
8 # converted to bash v2 syntax by Chet Ramey
10 TMPFILE
=${TMP:-/tmp}/shprof$$
12 trap 'rm -f $TMPFILE' EXIT
20 # create script with profiling enabled
21 cat > $TMPFILE <<- \_EOF_
27 *) file=$(type -path "$1") ;;
30 echo "*** line profile for $file ***"
32 while read -r && [ $i -le $NLINE ]; do
34 if [ "$count" -gt 0 ]; then
35 echo "[$count] $i: $REPLY"
40 # make the profiling script remove itself after printing line stats
41 echo "rm -f $TMPFILE" >> $TMPFILE
42 cat >> $TMPFILE <<- \
_EOF_
47 NLINE=$(wc -l < "$_command")
48 while [ $i -le $NLINE ]; do
53 trap "_profend ${_command}" EXIT
54 trap '_line[$LINENO]=$((${_line[$LINENO]} + 1))' DEBUG
60 *) file=$((type -path "$1")) ;;
63 cat "${file-$1}" >> $TMPFILE || errexit "${1}: cannot open"
66 exec -a "$file" $TMPFILE "$@"