#! /bin/sh # example: find ./ -name "*.[ch]pp" -exec cat {} \; | sh ./code-stat.sh set -e bc --version >/dev/null awk --version >/dev/null file=$(mktemp) cat > $file without_spaces() { grep -v "^[ ]*$"; } without_brackets() { grep -v "^ *[()] *$" | grep -v "^ *); *$" | grep -v "^ *[}{] *$" | grep -v "^ *}; *$"; } without_directives() { grep -v "^ *#"; } structures() { grep "struct \| class" | grep -v ";"; } all_size=$(cat $file | wc -l) without_spaces_size=$(cat $file | without_spaces | wc -l) without_brackets_size=$(cat $file | without_brackets | wc -l) without_directives_size=$(cat $file | without_directives | wc -l) structures_size=$(cat $file | structures | wc -l) pure_code_size=$(cat $file | without_spaces | without_brackets | without_directives | wc -l;) echo -n "Empty lines: " echo "($all_size - $without_spaces_size) / $all_size" | bc -l echo -n "Empty lines with brackets: " echo "($all_size - $without_brackets_size) / $all_size" | bc -l echo -n "Preprocessor directives: " echo "($all_size - $without_directives_size) / $all_size" | bc -l echo -n "Lines per classes: " echo "$pure_code_size / $structures_size" | bc -l pure_code_chars=$(cat $file | without_spaces | without_brackets | without_directives | wc -m) echo -n "Lines average length: " echo "$pure_code_chars / $pure_code_size" | bc -l rm $file