bash3boilerplate/test/style.pl
Izaak Beekman 7cf9ea708d Issue 44 issue 21 issue 47 ()
* Fix shifting over `--`: don't throw errexit

 Fixes 

* Add error trapping (including Ctrl-C) info to FAQ

 Closes 

* Add backtracing to help localize errors

 - Fixes 
 - Backtracing is turned on when the debugging `-d` flag is passed,
   otherwise backtracing function defined but signal trap not set.
 - Update main-help fixture due to moving trap

* Untabify main.sh

 Might be a controversial move, but I loath tabs...

* Add checks for tab chars and trailing whitespace

 - Update poor-man's style enforcement script to check for these
   violations
 - Better document style guidlines in README with small tweaks

* Add a magic variable to indicate if being source

 if `__i_am_main_script=1 #true` then `main.sh` called directly
 Fixes 
2017-02-10 15:03:07 +01:00

46 lines
1.1 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
die "usage: $0 <file>\n" if (not @ARGV);
my $rc = 0;
my $file = shift;
open(my $fh, '<', $file) or die "Cannot open \`$file' for read: $!\n";
while (<$fh>) {
next if (/^\s*#/);
my $errors = 0;
# remove everything between single quotes
# this will remove too much in case of: echo "var='$var'"
# and thus miss an opportunity to complain later on
# also it mangles the input line irreversible
s/'[^']+'/'___'/g;
# highlight unbraced variables--
# unless properly backslash'ed
$errors += s/((?:^|[^\\]))(((\\\\)+)?\$\w)/$1\033[31m$2\033[0m/g;
# highlight single square brackets
$errors += s/((?:^|\s+))\[([^\[].+[^\]])\](\s*(;|&&|\|\|))/$1\033[31m\[\033[0m$2\033[31m\]\033[0m$3/g;
# highlight double equal sign
$errors += s/(\[\[.*)(==)(.*\]\])/$1\033[31m$2\033[0m$3/g;
# highlight tabs mixed with whitespace at beginning of lines
$errors += s/^( *)(\t+ *)/\033[31m\[$2\]\033[0m/;
# highlight trailing whitespace
$errors += s/([ \t]+)$/\033[31m\[$1\]\033[0m/;
next if (not $errors);
print "${file}[$.]: $_";
$rc = 1;
}
close($fh);
exit $rc;