问题
Im trying to do a full back up and copy over all files from one directory into another directory
#!/bin/bash
#getting files from this directory
PROJECTDIRECTORY=/../../Project3
#Copied to this directory
FILEBACKUPLOCATION= /../.../FMonday
for FILENAME in $PROJECTDIRECTORY/*
do
cp $FILENAME $FILEBACKUPLOCATION
done
But I keep getting this error
./fullbackup: line 6: /../../FMonday: Is a directory
cp: missing destination file operand after
回答1:
Variable assignments in bash scripts require no space between the variable name and value or (unless quoted) within the value. Since a space was present in the line FILEBACKUPLOCATION= /../.../FMonday, it attempted to execute /../.../FMonday as a command (which caused the first error) with FILEBACKUPLOCATION assigned to an empty string. The variable was then not assigned when the other command further down tried to use it (accounting for the second error).
回答2:
I think this is what you need is FILEBACKUPLOCATION=/FMonday/
回答3:
In my case, I just used used space and dot operator after the file name. It worked perfectly.
For Example, darthVader is a file name which is inside F drive. So, I used the command "mv /f/darthVader . "
回答4:
Has you already found the variable needs to be FILEBACKUPLOCATION=/location with no space, also if possible try to make your script a little more portable by using something like:
FILEBACKUPLOCATION=$HOME/backup/FMonday
In this case, the location is relative to your user $HOME environment variable.
Going further, if you want to keep 2 files in sync probably you could do better with rsync one of the advantages is that it will only copy the files that don't exist or update the ones that have been changed.
You indeed could create an alias to copy/sync files on demand, for example:
alias cpr="rsync --delete --archive --numeric-ids --human-readable --no-compress --whole-file --verbose --info=progress2"
Then if you would like to sync contents of /dir/foo into /dir/bar, could simply do:
$ cpr /dir/foo/ /dir/bar/
Your script also could then be something like:
#!/bin/sh
ORIGIN=/path/to/foo/
DESTINATION=/path/to/bar/
rsync --delete \
--archive \
--numeric-ids \
--human-readable \
--no-compress \
--whole-file \
--verbose \
--info=progress2 \
$ORIGIN $DESTINATION
Notice that in this case the options --no-compress and --whole-file are used, mainly because the files will be copied locally, if this where a remote server options could be different, check this post (The fastest remote directory rsync over ssh archival I can muster (40MB/s over 1gb NICs)
来源:https://stackoverflow.com/questions/20324077/cp-missing-destination-file-operand-after