Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In this code, the excludespec variable is expected to match all it substring, but it appears to evaluate the to the actual files matching it when it is executed, rather than pass its exact representation to the tar command using it.

excludespec=${PWD##*/}\_$USER\_hostname.bkcd_backup*

The end result is the archive being created does not match the exclusion list so tar outputs:

tar: .: file changed as we read it

Is the string defining it doing something I don't know about?

Here is the code:

#!/bin/sh
# bkcd - backup current directory in situ

DMY_M() {
  date +%Y%m%d_%H%M
}

timestring=$(DMY_M)
echo `pwd` - $timestring > .bkcdspec  
filename=${PWD##*/}\_$USER\_`hostname`.bkcd_backup.$timestring.tar.gz    
excludespec=${PWD##*/}\_$USER\_`hostname`.bkcd_backup*
fullexclude="$excludespec"tar.gz    
echo excludespec - $excludespec
echo filename - $filename
echo fullexclude - $fullexclude    
tar -cpzf $filename --exclude=$fullexclude .
rm .bkcdspec
share|improve this question
    
Assuming that /bin/sh points to /bin/dash (it usually does), your script isn't a bash script, it's a dash script. –  Dennis Jan 21 '13 at 1:48
1  
You don't need to ask the same question twice –  glenn jackman Jan 21 '13 at 2:22
add comment

1 Answer

up vote 2 down vote accepted

Wildcarding is done after variable substitution. To paste in the literal value of a substitution but prevent wildcarding, use double quotes around the $var reference.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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