shell-base/modules/scripting/minimize.func

47 lines
1.3 KiB
Bash

#!/bin/bash
minimize () {
# will minimize script, removing comment lines and blank lines and inline comments
# TODO -r remove shebang, replace newlines with ; remove continuation \
# usage: <options> "source" (source as text or source as filename, as text MUST be quoted!)
# options:
# -t # argment will get text of source, otherwise it is the file path
# -o filepath # output to file
# -v # verbose, only applies when using -o. will output to file and return (stdout).
local out; local verbose; local text
local min="/^[[:space:]]*#/d; /#$/d; /^$/d"
local OPTION; local OPTARG; local OPTIND
while getopts 'to:v' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
t)
text=true
;;
v)
verbose=true
;;
o)
out="$OPTARG"
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $((OPTIND - 1))
if [[ $text ]]; then
res="$(echo "$1" | sed "$min")"
else
res="$(sed "$min" "$1")"
# ; s/\(.*\)#.*/\1/"
# | sed -z 's/\n/;/g' | sed 's/;$/\n/' | tr -s ' ' | sed 's/{;/{\n/g' | sed 's/;};/\n}\n/g')"
fi
out=${out:-$2}
if [[ $out ]]; then
[[ $verbose ]] && echo "$res" | tee "$out" || echo "$res" > "$out"
else
echo "$res"
fi
}