Skip to main content
2 of 4
added 285 characters in body
Dagg
  • 4.6k
  • 1
  • 25
  • 41

Shell script for creating CR questions

Inspired by Tool for creating CodeReview questions.

This script generates a markdown document suitable for a CR question from a list of files. For each file it finds, it sends the following to stdout:

  • The relative path to the file, in bold.
  • The byte count and line count, in parentheses.
  • The contents of the file, indented four spaces on each line.

./mdproj.sh (186 bytes in 7 lines)

for path in $(find "$@"); do
    bytes="$(wc -c < "$path") bytes"
    lines="$(wc -l < "$path") lines"
    echo -e "\n**$path** ($bytes in $lines)\n"
    sed "s/^/    /" < "$path"
done

The script takes the same options as find. Example usage:

mdproj.sh -name *.h -or -name *.cpp > whatever.md

Update / known issues

  • No spaces, backquotes, backslashes, asterisks, or other weird characters are allowed in file names.

  • Watch out for shell expansion when invoking the script. Arguments should be enclosed in quotes in cases where the the shell would expand them.

Dagg
  • 4.6k
  • 1
  • 25
  • 41