3
\$\begingroup\$

Fairly easy way to compress a wordpress plugin (or similar) as a zip file without including all the dependencies used to create and manage the development, but which don't belong in the final product.

#!/bin/bash
# This script zips a directory, excluding specified files, types and subdirectories.
#  while zipping the directory it excludes hidden directories and certain file types

[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.bash_profile

DIRECTORY=$(cd `dirname $0` && pwd)

if [[ -z $1 ]]; then
  echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
  DIRECTORY_TO_COMPRESS=${1%/}
  ZIPPED_FILE="$2.zip"

  COMPRESS_IGNORE_FILE=("\.git" "*.zip" "*.csv" "*.json" "gulpfile.js" "*.rb" "*.bak" "*.swp" "*.back" "*.merge" "*.txt" "*.sh" "bower_components" "node_modules") 
  COMPRESS_IGNORE_DIR=("bower_components" "node_modules")

  IGNORE_LIST=("*/\.*" "\.* "\/\.*"")
  if [[ -n $COMPRESS_IGNORE_FILE ]]; then
      for IGNORE_FILES in "${COMPRESS_IGNORE_FILE[@]}"; do
          IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_FILES/*")  
      done
      for IGNORE_DIR in "${COMPRESS_IGNORE_DIR[@]}"; do
          IGNORE_LIST+=("$DIRECTORY_TO_COMPRESS/$IGNORE_DIR/")
      done
  fi

  zip -r "$ZIPPED_FILE" "$DIRECTORY_TO_COMPRESS" -x "${IGNORE_LIST[@]}" # >/dev/null
  # echo zip -r "$ZIPPED_FILE" "$DIRECTORY_TO_COMPRESS" -x "${IGNORE_LIST[@]}" # >/dev/null
  echo $DIRECTORY_TO_COMPRESS "compressed as" $ZIPPED_FILE.
fi
\$\endgroup\$
  • 1
    \$\begingroup\$ Any particular reason you're not using the git archive command? That would take care of much of the hard work for you, automatically excluding all ignored files \$\endgroup\$ – Stop ongoing harm to Monica Oct 11 '15 at 6:06
  • \$\begingroup\$ i wasn't aware that there was a git archive command. will look into it. \$\endgroup\$ – MikeiLL Oct 12 '15 at 14:13
2
\$\begingroup\$

I'm just going to poke at some of your unix conventions and validation.

Your general handling looks polished. I like the way that you handle whether this is an interactive terminal, but wonder whether it would simply be better to use the more standard check for $PS1 or the i flag ( reference here ). If you are going to use tty to tell, you should probably rely on the exit status rather than the text message.

The way you find the user directory is great, and is similar to what I normally do too. There are some variations on it, but it is more than good enough.

This next one is the one that concerns me most though:

if [[ -z $1 ]]; then
  echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
else
  .....

That code has three problems....

  1. your script takes 2 parameters, but you only check for 1.
  2. if the parameter is missing, you still exit with a success exit code (0). You should exit with something else .. (like 1).
  3. there is no need for an else condition. Treat it like a guard-clause...

This is how I would do it (and I would use $# and not $2 because having too many arguments is as bad as not having enough....

if [[ $# -ne 2 ]]; then
  echo "Usage: managed_directory_compressor /your-directory/ zip-file-name"
  exit 1
fi

Then there is no need to indent, or so on, for the remaining code.

The remaining code looks relatively good. There's no error handling on the zip operation though.

One last thing, your first comments are duplicated:

# This script zips a directory, excluding specified files, types and subdirectories.
#  while zipping the directory it excludes hidden directories and certain file types

If you're going to comment things, make them good.

\$\endgroup\$
  • \$\begingroup\$ Thanks for all the great feedback. To be honest, it's not my handling as much as my pasting together of bits of found code. I could really use a general intro to bash scripting. Would you mind explaining how exactly the first line of "my" code is handling the tty check. How it's telling if it's an interactive terminal, what else might be calling the script and how ~/.bash_profile ties into it. I also can't actually see what the second line ("The way you find the user directory is great"?) is doing. The rest I understand. \$\endgroup\$ – MikeiLL Jun 12 '15 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.