Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to write a shellscript which prints a tree-view for a specific directory and it's subdirs to pdf via latex, as well as the titles and contents of all scripts contained within that primary directory.

The treeview works like a charm, but I have no idea how to get the printing of the scripts to work.

Code thus far:

#!/bin/bash

# Script to export directory with pdflatex

# Generate .tex file
# Directory Listing
echo "\documentclass[11pt,a4paper,oneside]{article}" > tmp.tex
echo "\usepackage{fullpage}" >> tmp.tex
echo "\begin{document}" >> tmp.tex
echo "\section{Listing}" >> tmp.tex
echo "\begin{verbatim}" >> tmp.tex
tree $1 >> tmp.tex
echo "\end{verbatim}" >> tmp.tex
echo "\end{document}" >> tmp.tex

# ShellScript printout
???????

# Generate .pdf file
pdflatex tmp.tex

#Cleanup
rm tmp.tex
share|improve this question
    
I have no idea what you mean by “ShellScript printout”. What do you want that ??????? to do? –  Gilles Dec 13 '12 at 22:49
    
@Gilles I believe # ShellScript printout mean place to program a loop for printing each shell scripts... As using dirtree in an old script of mine seemed to me like a good idea, I've resurect and rewrite them for making a filesystem tree presentation of table of content ... –  F. Hauri Jan 30 '13 at 12:24
    
Maybe Doxygen can help: stack.nl/~dimitri/doxygen –  Bernhard Jan 30 '13 at 13:03
add comment

2 Answers

It is not exactly clear to me what you want, but as I understand it, you want to have the output of all .sh files printed into tmp.tex. Then you could do something like

find -name "*.sh" -exec cat "{}" + >> tmp.tex

Which concatenate all .sh files, and then adds it to tmp.tex.

share|improve this answer
    
Or just use cat **/*.sh >> tmp.tex if on Bash 4+ with option globstar on. –  Werner Lehmann Dec 29 '12 at 22:31
add comment

Pretty print directory tree and script files contents

Edit: Newer version with full table of content as tree and picture support, in second part.

Using \verbatiminput from package verbatim.

Like this:

#!/bin/bash

tempfile=$(mktemp /tmp/dirtree-XXXXX.tex)
trap "rm $tempfile" 0 1 2 3 6 9 15

cat <<EOF >$tempfile
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage,verbatim,dirtree}
\begin{document}
\section{Listing}
\dirtree{%
EOF

export -a scriptList=()
while IFS=/ read -a fPath ;do
    file="${fPath[*]:${#fPath[*]}-1}"
    IFS=/
    full="${fPath[*]}"
    type="$(file -b "$full")"
    echo .${#fPath[@]} "${file//_/\\_}\DTcomment{$type}. "
    [[ "$type" =~ script.text ]] && scriptList=("${scriptList[@]}" "$full")
    done  < <(
    find $1 -type d -o -type f
)  >>$tempfile

export IFS=$'\n\t '
echo "}" >>$tempfile

for file in "${scriptList[@]}";do
    name="${file##*/}"
    printf "\\section{%s}\n{\\\\scriptsize\\\\verbatiminput{%s}}\n" \
    "${name//_/\_}" "${file}"  >>"${tempfile}"    
done

echo >>"${tempfile}" '\end{document}'

pdflatex -interaction nonstopmode "${tempfile}"

Who would produce this kind of output:

print directory content

Pretty print directory tree with table of content, scripts and images files.

NOTA: For computing toc, latex have to be run two times.

bugs:

This script is only a proof of concept, type of images is probably limited and could be improved, eventualy by the help of imagemagik, netpbm or any graphic lib... and so on

todo:

  • fix dimensions of images
  • improve images filtering
  • add support for pdf, ps and maybe other printable like .man, .tex, .sgml, .odf
    • add option for printing first page of document files.
  • make and purge temporary files more properly.

There it is:

#!/bin/bash

tempfile=$(mktemp /tmp/dirtree-XXXXX.tex)
# trap "rm $tempfile" 0 1 2 3 6 9 15

cat <<EOF >$tempfile
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage,graphicx,verbatim,dirtree}
\makeatletter
\newcommand{\typePPage}[2]{\DTcomment{{\scriptsize #1
\begin{minipage}[t]{5em}\mbox{}\hfill\ifx\@empty#2\else%
s.$\ref{sec:#2}$, p.$\pageref{sec:#2}$\fi\end{minipage}}}}
\makeatother
\begin{document}\parindent=0pt%
\section{Listing}
\dirtree{%
EOF

export -a scriptList=()
export -A typelist=()
while IFS=/ read -a fPath ;do
    file="${fPath[*]:${#fPath[*]}-1}"
    IFS=/
    full="${fPath[*]}"
    type="$(file -b "$full")"
    if [[ "$type" =~ script.text ]] || [[ "$type" =~ image ]] ;then
    scriptList=("${scriptList[@]}" "$full")
    typelist["${full//\//_}"]="$type"
    echo .${#fPath[@]} \
        "${file//_/\\_}\typePPage{$type}{${file//[\/.+()_-]/}}. "
    else
    echo .${#fPath[@]} "${file//_/\\_}\typePPage{$type}{}. "
    fi
    done  < <(
    find $1 -type d -o -type f
)  >>$tempfile

export IFS=$'\n\t '
echo "}" >>$tempfile

for file in "${scriptList[@]}";do
    name="${file##*/}"
    printf '\\section{%s}\n\\label{sec:%s}\n' \
    "${name//_/\_}" "${name//[\/.+()_-]/}"
    if [[ "${typelist["${file//\//_}"]}" =~ script.text ]];then
    printf '{\\scriptsize\\verbatiminput{%s}}\n' "${file}"
    else
    printf '\\includegraphics[width=\\textwidth]{%s}\n' "${file}"
    fi
done >>"${tempfile}"

echo >>"${tempfile}" '\end{document}'

pdflatex -interaction nonstopmode "${tempfile}" >/dev/null 2>&1
pdflatex -interaction nonstopmode "${tempfile}"

Could produce:

Sample of output

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.