I was curious how many lines were in the new OurUNO rewrite, so I decided to write a little script to find out. Well, that all got out of hand and I kept adding things and masks and depth recursion limiting and I managed to stop myself before I added color to the script, so I did okay I guess.
Anyway, here it is. I’m sure there is some really clever way to do an equivalent one-liner of this, but hey, that’s life.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/bin/sh function printUsage { echo "Usage: countLines directory [options]" echo echo "Options:" echo " -m=XX --mask=XX - The mask may be any grep style regular expression." echo " -d=XX --depth=XX - The maximum depth of recursion. Defaults to 20." echo exit } if [ $# -le 0 ]; then printUsage exit fi if [ "$1" == "-v" ]; then printUsage exit fi TOTALCOUNT=0 FILEMASK='' MAXDEPTH=20 # Drag out our options... for i in $@; do if [ `echo $i | sed 's/^\(-m=\).*$/\1/'` == "-m=" ]; then FILEMASK=`echo $i | sed 's/^-m=\(.*\)$/\1/'` elif [ `echo $i | sed 's/^\(--mask=\).*$/\1/'` == "--mask=" ]; then FILEMASK=`echo $i | sed 's/^--mask=\(.*\)$/\1/'` elif [ "$i" == "$1" ]; then continue; elif [ `echo $i | sed 's/^\(-d=\)[0-9]*$/\1/'` == "-d=" ]; then MAXDEPTH=`echo $i | sed 's/^-d=\(.*\)$/\1/'` elif [ `echo $i | sed 's/^\(--depth=\)[0-9]*$/\1/'` == "--depth=" ]; then MAXDEPTH=`echo $i | sed 's/^--depth=\(.*\)$/\1/'` else printUsage exit fi done CURDEPTH=0 function wcDir { FILES=`ls -l $1 | grep ^- | awk '{print $8}' | grep -e "$FILEMASK"` for i in $FILES; do LINES=`wc -l $1/$i | awk '{print $1}'` TOTALCOUNT=$(($LINES + $TOTALCOUNT)) done } function recurseDir { COUNT=`ls -l $1 | grep ^d | awk '{print $8}' | wc -l` CURDEPTH=$(($CURDEPTH + 1)) if [ $COUNT != 0 ] && [ $CURDEPTH -lt $MAXDEPTH ]; then for i in `ls -l $1 | grep ^d | awk '{print $8}'`; do recurseDir $1/$i done fi wcDir $1 CURDEPTH=$(($CURDEPTH - 1)) } recurseDir $1 echo $TOTALCOUNT |
Bonus! Here’s a tip for posting scripts on the interwebs. Replace your tabs with spaces before copying them into your posts with:
$ cat scriptOrCodeSource | sed 's/\t/ /g'