Michael Garrison
Michael Garrison

Reputation: 941

Script won't recognize the file / directory

For class we have to work on a remote server that the school hosts. So far I have made a lot of files on the server and I would like to back them up in case I want to transfer them to my laptop or in case I accidentally delete a directory or make a silly error. I found a tutorial and a script to back up the file and I decided to modify it so that it would determine what directory it's in (which will be the main user's) and the cd to the Documents. It also creates the directory Backups if it doesn't exist. I am still pretty new to this sort of scripting and any additional advice or post links would be greatly appreciated.

Code:

#!/bin/bash
#######################################################
##       Simple backup script..   
## Created by Matthew Brunt: ([email protected])
## Licensed under GNU GPL v3 or later, at your option.
##   http://www.gnu.org/licenses/gpl.html
##
## Further edited by Michael Garrison to backup the 
## directory it is located in and print the contents.
#######################################################

mkdir -p Backup

#Defines our output file
OUTPUT= $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz

#Defines our directory to backup
BUDIR=$( cd Desktop && pwd )
#Display message about starting the backup
echo "Starting backup of directory $BUDIR to file $OUTPUT"

#Start the backup
tar -cZf $OUTPUT $BUDIR

#Checking the status of the last process:
if [ $? == 0 ]; then
        #Display confirmation message
    echo "The file:"
        echo $OUTPUT
    echo "was created as a backup for:"
        echo $BUDIR

        echo ""
    echo "Items that were backed up include:"

    for i in $BUDIR; do
            echo $i
    done
        echo ""
else
    #Display error message message
        echo "There was a problem creating:"
        echo $OUTPUT
    echo "as a backup for:"
    echo $BUDIR
fi

I know that the original script works and it worked until I changed the $OUTPUT variable. I currently get the following result:

./backup.sh
./backup.sh: line 15: /Users/mgarrison93/Backup/backup_20121004.tar.gz: No such file
or directory
Starting backup of directory /Users/mgarrison93/Desktop to file 
tar: no files or directories specified
There was a problem creating:

as a backup for:
/Users/mgarrison93/Desktop

I can see that it is not accepting the file name, but I don't know how to correct this.

I just tried changing $OUTPUT to /Backups/file-name.tar.gz which I originally had and it works fine. The problem seems to be $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz. Just not sure what is wrong.

Upvotes: 1

Views: 697

Answers (1)

David Z
David Z

Reputation: 131560

Consider these two entirely different pieces of bash syntax: first, you have the syntax for setting a variable to a value permanently (in the current script),

<variable>=<value>

and then there is the syntax for running a command with a variable temporarily set to a value ,

<variable>=<value> <command> <argument> ...

The difference between these two is the space. After the =, once bash runs into an unquoted space, it takes that to mean that the <value> has ended, and anything after it is interpreted as the <command>.

In this line of your script,

OUTPUT= $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz

you have a space after OUTPUT=. bash interprets that to mean that OUTPUT is to be (temporarily) set to the empty string, and the rest of the line, i.e. the result of $( cd Backup && pwd )/backup_$(date +%Y%m%d).tar.gz, is a command and arguments to be run while OUTPUT is equal to the empty string.

The solution is to remove the space. That way bash will know that you're trying to assign the rest of the line as a value to the variable.

Upvotes: 2

Related Questions