Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # name: compress-bash-script.sh
- # version: 4.1.1, 27-aug-2020, by eibgrad
- # purpose: reduce size of bash script
- # notes:
- # - trailing comments are always preserved
- # - output is always to stdout
- # - input from stdin is optional:
- # cat </path/bash-script> | compress-bash-script.sh
- # --comp is the default compression level
- comp=
- # function usage()
- usage() {
- echo 'Usage: compress-bash-script.sh [options] [</path/bash-script>]'
- echo
- echo ' Reduce size of bash script.'
- echo
- echo ' Options:'
- echo ' --nocom remove blank lines and pure/non-functional comments'
- echo ' --comp (default) same as --nocom, plus remove leading whitespace'
- echo ' -h,--help this usage information'
- echo
- echo ' NOTE: If file not specifed, input is expected from stdin.'
- echo
- }
- # handle -h|--help option/argument
- for opt; do case $opt in -h|--help) usage; exit 0;; esac; done
- # process command line options/arguments
- while [ $# -gt 0 ]; do
- case "$1" in
- --nocom) nocom=; unset comp;;
- --comp) comp=; unset nocom;;
- *) break 2;;
- esac
- shift
- done
- if [ "$1" ]; then
- if [ -f "$1" ]; then
- # redirect file to stdin
- exec "$0" $([ ${nocom+x} ] && echo --nocom) < "$1"
- else
- echo "error: file not found: $1"
- exit 1
- fi
- fi
- # option nocom: remove blank lines and pure/non-functional comments
- [ ${nocom+x} ] && cat - | sed -r '/^[[:space:]]*($|#([[:space:]]|#|$))/d'
- # option comp: same as --nocom, plus remove leading whitespace
- [ ${comp+x} ] && cat - | sed -r 's/^[[:space:]]*//;/^($|#([[:space:]]|#|$))/d'
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement