Quick
index
main
eev
eepitch
maths
angg
blogme
dednat6
littlelangs
PURO
(C2,C3,C4,
 λ,ES,
 GA,MD,
 Caepro,
 textos,
 Chapa 1)

emacs
lua
(la)tex
maxima
 qdraw
git
lean4
agda
forth
squeak
icon
tcl
tikz
fvwm
debian
irc
contact

An example of a bash script with test blocks (2024)

Here is a screenshot - click on it to enlarge:

It shows a script that I use to convert a series of PDFs into an animated gif. It is explained, as a tool, in this section of one of my pages about Maxima.

I don't write Bash code often, and I would never be able to remember what the tricks in red below do

and_run      () { echo $*; $*; }
cdr          () { echo ${@:2}; }
add_1zs      () { for i in $*; do echo $i 1-z; done; }
makeboth     () { $ECHO makeflipbook $(cdr $*);
                  $ECHO makeanim $1 $2; }
makeflipbook () { $ECHO qpdf --empty --pages $(add_1zs $(cdr $*)) -- $1; }
makeanim     () {
  $ECHO convert -alpha deactivate ${VERBOSE:+-verbose} \
                -delay $DELAY -loop 0 -density $DENSITY $2 $1
}

without 1) links to the documentation and 2) examples. The links to the documentation appear right above that code in the screenshot - let me copy them here:

# See:
#   (find-bashnode "Environment" "parameter assignments")
#   (find-bashnode "Shell Parameter Expansion" "${@:7}")
#   (find-bashnode "Shell Parameter Expansion" "${PARAMETER:+WORD}")

These links work much better in Emacs than in the browser. If you click on the `")' at the end of one of the `find-bashnode' links above you will see that it goes to a page of the Bash manual, but it "ignores the pos-spec-list"... while in Emacs the second link searches for the first occurrence of "${@:7}", as shown here:

So that solves the first half of my problem - "1) links to the documentation".

The second half of my problem - "2) examples" - is much trickier. For me it is much easier to test the individual functions in a bash script if it can be used both "as a library" and "as a script"; and the trick that lets it be used in both ways is an

eval "$*"

at the end of the script - see here, or in this screenshot, that also shows its comments and a test block:




Test:

* (eepitch-shell)
* (eepitch-kill)
* (eepitch-shell)
rm -fv    /tmp/mkanim1.sh
wget -P   /tmp/ http://anggtwu.net/myqdraw/mkanim1.sh
chmod 755 /tmp/mkanim1.sh
# (find-anchor "/tmp/mkanim1.sh")
# (find-anchor "/tmp/mkanim1.sh" "makeanim-tests")
# (find-anchor "/tmp/mkanim1.sh" "latex-tests")
# (find-anchor "/tmp/mkanim1.sh" "eval-tests")