0

I have created my first bash script today which basically creates a zip file of the directory in which the bash script sits. I can get the zip file to create just fine. But I am asking the user to specify which directories he wants to exclude from the zip (for example, on a wordpress website, the uploads folder can get very big so when creating the zip sometimes I would like to exclude that from the zip folder that gets created). I know that the following command works in this manner when run directly on the command line:-

zip -r new_public_html.zip . -x ./wp-content/uploads/**\*

However, my bash script asks the user to specify directories that he wants to exclude from the zip and I can see the value stored in the variable $THE_DIRECTORIES_TO_EXCLUDE but for some reason when the zip file gets created it includes the uploads folder. I tried all of the statements which have been commented out in the bash script below - and none of them worked. As I say, I can get the zip file but I cannot exclude the folder the user specifies when asked. Any help would be greatly appreciated.

#!/bin/bash

read -p "Please specify the directories contained within wp-content that you would like to exclude.  If you specify more than one directory, separate each exclusion by a space: " DIRECTORIES_TO_EXCLUDE

THE_DIRECTORIES_TO_EXCLUDE=""
for directory_to_exclude in $DIRECTORIES_TO_EXCLUDE
do
    THE_DIRECTORIES_TO_EXCLUDE="$THE_DIRECTORIES_TO_EXCLUDE ./wp-content/$directory_to_exclude/**\*"
done

#zip -r new_public_html.zip . -x${THE_DIRECTORIES_TO_EXCLUDE}
#zip -r new_public_html.zip . -x"$THE_DIRECTORIES_TO_EXCLUDE"
#zip -r new_public_html.zip . -x ./wp-content/uploads/**\*
#zip -r new_public_html.zip . -x"${THE_DIRECTORIES_TO_EXCLUDE}"
#zip -r new_public_html.zip . -x eval "$THE_DIRECTORIES_TO_EXCLUDE"
#zip -r new_public_html.zip . -x eval $THE_DIRECTORIES_TO_EXCLUDE

#cmd='zip -r new_public_html.zip . -x "$THE_DIRECTORIES_TO_EXCLUDE"'
#eval "$cmd";

#newcmd=$(zip -r new_public_html.zip . -x$THE_DIRECTORIES_TO_EXCLUDE)
#echo newcmd;

echo "directories to exclude have been set to : $THE_DIRECTORIES_TO_EXCLUDE"
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Diesel
  • 1
  • 2
  • typically you pass these options as arguments not interactive. – Something Something Sep 24 '22 at 13:00
  • do you need to use zip? can you use tar? – Something Something Sep 24 '22 at 13:01
  • Lists should not be stored or passed as strings. Use arrays instead. – Charles Duffy Sep 24 '22 at 13:06
  • Anyhow -- `**` only works in context where you turned on the `globstar` command. It's turned off by default in scripts. (It's _also_ turned off by default for interactive shells, but a lot of distros' default dotfiles turn it back on). – Charles Duffy Sep 24 '22 at 13:07
  • See [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050) for more on the "don't use strings to store argument lists" bit. – Charles Duffy Sep 24 '22 at 13:08
  • ...whereas the linked duplicate is focused narrowly on the "why does this behave differently between a script and an interactive interpreter?" problem (being that `globstar` needs to be enabled to make it work in a script). – Charles Duffy Sep 24 '22 at 13:11
  • that said, of all your attempts, `zip -r new_public_html.zip . -x ./wp-content/uploads/**\*` is the best of them. If you were to use a variable, you'd want to make it an array; that would be `excluded_uploads=( ./wp-content/uploads/**\* )` to assign, then `zip -r new_public_html.zip . -x "${excluded_uploads[@]}"` to expand. Again, though, you need to turn on globstar _before_ the assignment. – Charles Duffy Sep 24 '22 at 13:12

0 Answers0