Added gitignore entries needed to ignore derived objects generated from full build...
[bash.git] / examples / scripts.v2 / shprof
blob73a1bb97494086618389051b3e4c892d4886a5c3
1 #! /bin/bash
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
14 errexit()
16 echo $0: "$@" >&2
17 exit 1
20 # create script with profiling enabled
21 cat > $TMPFILE <<- \_EOF_
22 declare -a _line
23 _profend()
25 case "$1" in
26 /*|./*) file="$1" ;;
27 *) file=$(type -path "$1") ;;
28 esac
30 echo "*** line profile for $file ***"
31 i=1;
32 while read -r && [ $i -le $NLINE ]; do
33 count=${_line[$i]}
34 if [ "$count" -gt 0 ]; then
35 echo "[$count] $i: $REPLY"
37 i=$((i + 1))
38 done <$file
39 _EOF_
40 # make the profiling script remove itself after printing line stats
41 echo "rm -f $TMPFILE" >> $TMPFILE
42 cat >> $TMPFILE <<- \_EOF_
44 _command=$1
45 shift
46 i=1
47 NLINE=$(wc -l < "$_command")
48 while [ $i -le $NLINE ]; do
49 _line[$i]=0
50 i=$((i + 1))
51 done
52 unset i
53 trap "_profend ${_command}" EXIT
54 trap '_line[$LINENO]=$((${_line[$LINENO]} + 1))' DEBUG
55 LINENO=0
56 _EOF_
58 case "$1" in
59 /*|./*) file=$1 ;;
60 *) file=$((type -path "$1")) ;;
61 esac
63 cat "${file-$1}" >> $TMPFILE || errexit "${1}: cannot open"
64 chmod +x $TMPFILE
66 exec -a "$file" $TMPFILE "$@"