[RndTbl] Fun with BASH

Geoffrey Jacobs gdjacobs at curets.ca
Wed May 15 22:03:59 CDT 2019


Just to follow up on the discussion of interesting BASH tricks last 
night, here is how one could implement file system traversal if find 
didn't exist. Note that GLOBIGNORE isn't always required to be 
explicitly set as shown for the root and parent directory to be 
excluded, but this does as required and provides clarity.

#!/bin/bash
GLOBIGNORE=". .."
# Ignore the current and parent directory
# This will be implicit with GLOBIGNORE not null, although . and .. can still
# be explicitly selected if nullglob and failglob aren't set

descend() {
     for i in * .*; do
         if [ -d "$i" ]; then
             pushd "$i"
             descend
             # Do things to files in the directory here
             popd
         fi
     done
}

descend
# end script

Also, Douglas Adams on the Galactic Presidency is translated by the jive 
filter:

Anyone willin' an' capable t'git demselves elected President by no
means should be allowed t'do de job.  What it is, Mama!

Or Hanlon's Razor as presented by a Swedish chef:

Nefer ettreeboote-a tu meleece-a thet vheech is edeqooetely ixpleeened
by stoopeedity.

Yes, the filters package is endless fun.



More information about the Roundtable mailing list