bash cp:缺少目标文件操作数之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20324077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:29:15  来源:igfitidea点击:

cp: missing destination file operand after

bashunixterminal

提问by user3019469

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

回答by user3019469

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 /../.../FMondayas a command (which caused the first error) with FILEBACKUPLOCATIONassigned 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).

bash 脚本中的变量赋值要求变量名和值之间或(除非引用)值之间没有空格。由于行中存在空格FILEBACKUPLOCATION= /../.../FMonday,因此它尝试/../.../FMonday作为FILEBACKUPLOCATION分配给空字符串的命令(导致第一个错误)执行。当另一个命令进一步尝试使用它时,变量没有被分配(考虑到第二个错误)。

回答by slayedbylucifer

I think this is what you need is FILEBACKUPLOCATION=/FMonday/

我认为这就是你需要的是 FILEBACKUPLOCATION=/FMonday/

回答by Sabbir Ahmed

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 . "

例如,darthVader 是 F 盘内的文件名。所以,我使用了命令“mv /f/darthVader”。

回答by nbari

Has you already found the variable needs to be FILEBACKUPLOCATION=/locationwith no space, also if possible try to make your script a little more portable by using something like:

您是否已经发现变量需要FILEBACKUPLOCATION=/location没有空格,如果可能的话,尝试使用以下内容使您的脚本更具可移植性:

FILEBACKUPLOCATION=$HOME/backup/FMonday

In this case, the location is relative to your user $HOMEenvironment variable.

在这种情况下,该位置与您的用户$HOME环境变量相关。

Going further, if you want to keep 2 files in sync probably you could do better with rsyncone of the advantages is that it will only copy the files that don't exist or update the ones that have been changed.

更进一步,如果你想保持 2 个文件同步,你可以做得更好,rsync其中一个优点是它只会复制不存在的文件或更新已更改的文件。

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/foointo /dir/bar, could simply do:

然后,如果您想同步/dir/foointo 的内容/dir/bar,只需执行以下操作:

$ 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-compressand --whole-fileare 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)

请注意,在这种情况下,使用了选项--no-compress--whole-file,主要是因为文件将被复制到本地,如果远程服务器选项可能不同,请查看这篇文章(我可以召集的最快的 ssh 存档上的远程目录 rsync (40MB/s)超过 1GB 的 NIC)