shell-base/modules/scripting/minimize.func

45 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/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; s/\(.*\)#.*/\1/"
local OPTION; local OPTARG; local OPTIND
# host=$(sed 's/\(.*\):.*/\1/' <<<"$SRC")
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")"
fi
if [[ $out ]]; then
[[ $verbose ]] && echo "$res" | tee "$out" || echo "$res" > "$out"
else
echo "$res"
fi
}