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
asked Jun 12, 2015 at 2:40
\$\endgroup\$
2
  • 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\$ Commented Oct 11, 2015 at 6:06
  • \$\begingroup\$ i wasn't aware that there was a git archive command. will look into it. \$\endgroup\$ Commented Oct 12, 2015 at 14:13

1 Answer 1

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.

answered Jun 12, 2015 at 3:46
\$\endgroup\$
1
  • \$\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\$ Commented Jun 12, 2015 at 7:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.