Bash shell 脚本语法问题:目标不是目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9812623/
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
Bash shell scripting syntactical issues: Target is not a directory?
提问by user1020069
I am attempting to copy a file to a bunch of folders present in a directory. The folders have been saved in propagation.txtand are like:
我正在尝试将文件复制到目录中的一堆文件夹中。文件夹已保存在propagation.txt,如下所示:
sfproject/folder1
sfproject/folder2
The code I am attempting to run is :
我试图运行的代码是:
for x in `cat propagation.txt`
do cp php.ini $x ; echo "Copied php.ini to $x"
done
echo "Finished";
However, it states that:
但是,它指出:
cp : 'target: 'propagation.txt' is not a text'
This is what propagation.txt consists of:
这是propagation.txt 的组成:
sfproject/apps/backend/modules/users/lib
sfproject/apps/backend/templates
sfproject/apps/frontend/config
sfproject/apps/frontend/lib
sfproject/apps/frontend/modules/EdboostSatGuide/actions
sfproject/apps/frontend/modules/EdboostSatGuide/templates
sfproject/apps/frontend/modules/dashboard/actions
sfproject/apps/frontend/modules/dashboard/templates
sfproject/apps/frontend/modules/quizzes/actions
sfproject/apps/frontend/modules/quizzes/templates
sfproject/apps/frontend/templates
sfproject/cache/frontend/prod/config
回答by Kaz
"Target is not a directory" is a cperror. It happens when you have three or more arguments, and the last argument is an existing filesystem object that isn't a directory!
“目标不是目录”是一个cp错误。当您有三个或更多参数时会发生这种情况,并且最后一个参数是一个现有的文件系统对象,而不是目录!
Of course
当然
cp a b
cp
works if bis a regular file, and not a directory. bis simply replaced with a. But
如果b是常规文件而不是目录,则有效。b简单地替换为a. 但
cp a b c
cp abc
means make a copy of aand binside directory c.
表示复制a和b内部目录c。
It's not clear how this problem could happen in your cp php.ini $x, because the loop variable $xis iterating over the results of a cat ...process substitution. The process substitution undergoes word splitting and the split pieces are assigned to xone by one. For your cpto end up with three or more arguments, $xwould have to somehow expand into two arguments.
目前尚不清楚这个问题是如何在您的 中发生的cp php.ini $x,因为循环变量$x正在迭代cat ...进程替换的结果。过程代换经过分词,分片被x一一分配。如果您cp最终得到三个或更多参数,则$x必须以某种方式扩展为两个参数。
Post the actual verbatim code that is failing and some sample data.
发布失败的实际逐字代码和一些示例数据。
On a related note, if you do have spaces in some of the path names, it will be best to put them on separate lines in propagation.txtand then process it like this:
在相关说明中,如果您在某些路径名中确实有空格,最好将它们放在单独的行中propagation.txt,然后像这样处理它:
# read dir names one by one and copy php.ini into them ...
while read dir ; do
cp php.ini "$dir"
# ...
done < propagation.txt # ... getting the names from propagation.txt
回答by pizza
It should work, change "do cp php.ini" to "do echo cp php.ini" and see what you get.
它应该可以工作,将“do cp php.ini”更改为“do echo cp php.ini”,然后看看你得到了什么。
回答by Gordon Davisson
Based on your comment to @pizza, I'm pretty sure your actual script is not what's in the question. I suspect you may have the wrong kind of quotes around cat propagation.txt, i.e.
根据您对@pizza 的评论,我很确定您的实际脚本不是问题所在。我怀疑你周围的引号可能是错误的cat propagation.txt,即
for x in 'cat propagation.txt' ; do cp php.ini $x ; echo "Copied php.ini to $x" ; done; echo "Finished";
instead of
代替
for x in `cat propagation.txt` ; do cp php.ini $x ; echo "Copied php.ini to $x" ; done; echo "Finished";
...or possibly some sort of "smart quote" or the like. I'll recommend using $()instead of backquotes; they do basically the same thing, but the syntax is cleaner if you're doing complex things and they're much harder to mistake for something else. So try this:
...或者可能是某种“智能报价”之类的。我会推荐使用$()而不是反引号;它们基本上做相同的事情,但是如果您正在做复杂的事情,则语法会更清晰,并且更难将它们误认为是其他东西。所以试试这个:
for x in $(cat propagation.txt) ; do cp php.ini $x ; echo "Copied php.ini to $x" ; done; echo "Finished"

![为什么 Bash 中的 '[' 和 ']' 周围应该有空格?](/res/img/loading.gif)