shzip2

10th October 2016 at 12:06am
bash CodeSnippets shzip

Small script to compress and obfuscate your shell scripts as base 64. Similar to shzip, but a slightly different implementation.

#!/bin/bash

main () {
  if [[ $# -ne 1 ]] || ! [[ -f "$1" ]] ; then
    >&2 echo "Syntax: ${0##*/} <script>"
    return 64
  fi
  printf '#!/bin/bash\n'
  printf 'eval "$(tail -n +3 "${BASH_SOURCE[0]}" | base64 -d | zcat)"; exit $?\n'
  gzip -c "$1" | base64
}

main "$@"

Example of the script in use:

nicolaw@host:~$ shzip2 helloworld
#!/bin/bash
eval "$(tail -n +3 "${BASH_SOURCE[0]}" | base64 -d | zcat)"; exit $?
H4sICJjZ+lcAA2hlbGxvd29ybGQAU1bUT8rM009KLM7gSk3OyFfISM3JyVcozy/KSeHKKeYCAPIx
zkUgAAAA
nicolaw@host:~$ shzip2 helloworld > compressed_script.sh
nicolaw@host:~$ bash compressed_script.sh
hello world
compressed_script.sh  helloworld
nicolaw@host:~$