1

I'm trying to make a bash script to lanch a tar command. I need the tar to have a variable parameter, but I can't have it work... Here it is:

i=1
for d in /home/test/*
do
    dirs[i++]="${d%/}"
done
echo "There are ${#dirs[@]} dirs in the current path"
for((i=1;i<=${#dirs[@]};i++))
do
        siteonly=${dirs[i]/\/home\/test\//}
        if [[ $siteonly == "choubijoux" ]]
            then
            exclude='--exclude "aenlever/*"';
        fi
    tar -czf /backups/sites/$siteonly.tar.gz ${dirs[i]} --exclude "tmp/*" --exclude "temp/*" --exclude "cache/*" $exclude
done

The tar command execute, but without the parameter --exclude "aenlever/*" So I suppose the variable is not taken into consideration... Is there a way to make it accept the variable as parameter?

4
  • 3
    Are you sure that the variable "exclude" is set at this point? Perhaps running with -x or echoing the tar command would help to make sure you are actually invoking it with the value. Commented Sep 10, 2013 at 17:43
  • Yes, the exclude is well placed, when I launch it in command line, it does its job Commented Sep 10, 2013 at 17:46
  • Not well placed, I was asking if it was set - i.e. defined. Commented Sep 10, 2013 at 17:50
  • Ah - I see. The problem is that you are passing two arguments (--exclude and "...") as a single arugment. tar is expecting them as seperate arguments. See post below for something that should work. Commented Sep 10, 2013 at 17:52

3 Answers 3

2

The better solution is to use an array:

        exclude=(--exclude "aenlever/*")
    fi
tar -czf /backups/sites/$siteonly.tar.gz ${dirs[i]} --exclude "tmp/*" --exclude "temp/*" --exclude "cache/*" "${exclude[@]}"

Also I think you need to reset the variable for every loop, but that depends on your intention.

for((i=1;i<=${#dirs[@]};i++))
do
    exclude=()

And I would suggest this simplified format as a whole:

#!/bin/bash

dirs=(/home/test/*)

# Verify that they are directories. Remove those that aren't.
for i in "${!dirs[@]}"; do
    [[ ! -d ${dirs[i]} ]] && unset 'dirs[i]'
done

echo "There are ${#dirs[@]} dirs in the current path."

for d in "${dirs[@]}"; do
    exclude=()
    siteonly=${d##*/}
    [[ $siteonly == choubijoux ]] && exclude=(--exclude "aenlever/*")
    tar -czf "/backups/sites/$siteonly.tar.gz" "$d" --exclude "tmp/*" --exclude "temp/*" --exclude "cache/*" "${exclude[@]}"
done
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That'it! Thanks ;-) And you're right, the variable have to be reset on each loop.
0

May be you would want to echo _${exclude}_ before tar to make sure the variable contains the value you expect.

Comments

0

You can use it like this:

exclude="aenlever/*"
tar -czf /backups/sites/$siteonly.tar.gz ${dirs[i]} --exclude "tmp/*" --exclude "temp/*" --exclude "cache/*" --exclude "$exclude"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.