Linux 如果从 shell 脚本执行,cp 命令将不会运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11328775/
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
cp command won't run if executed from shell script
提问by Kupe3
i have very simple shell script
我有非常简单的 shell 脚本
#!/bin/bash
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
echo "----"
echo "done"
but seems cp command fails
但似乎 cp 命令失败
if i execute
如果我执行
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
from terminal everything work ok. Can someone tell me how to include cp in shell script?
从终端一切正常。有人能告诉我如何在shell脚本中包含cp吗?
Thanks
谢谢
回答by unwind
回答by cdarke
We seem to have doubt as to how this script fails. If there is no error message then this is a strange one. I suggest:
我们似乎怀疑这个脚本是如何失败的。如果没有错误消息,那么这是一个奇怪的消息。我建议:
- On the command line (which works), do a
which cp
- Whatever the reply, then copy that and use it as the
cp
in the script (e.g./bin/cp
) - Check the widcard expansion, run your script with
bash -x script-name
and see if you get what you expect. echo $?
after the copy in the script - if it is zero then it (thinks it) worked.- Do a
ls -ld /var/www/ksite/app
from your script, maybe someone set a symbolic link? - If it still fails, source the script from the command-line and see if that works
. script-name
- Double check that the copy did actually fail! (maybe that should be step 1.)
- 在命令行(有效)上,执行
which cp
- 无论回复如何,然后复制它并将其用作
cp
脚本中的(例如/bin/cp
) - 检查通配符扩展,运行你的脚本,
bash -x script-name
看看你是否得到了你所期望的。 echo $?
在脚本中的副本之后 - 如果它为零,那么它(认为它)起作用了。ls -ld /var/www/ksite/app
从你的脚本中做一个,也许有人设置了一个符号链接?- 如果仍然失败,请从命令行获取脚本并查看是否有效
. script-name
- 仔细检查副本是否确实失败了!(也许这应该是第 1 步。)
回答by Levon
Just covering all the bases .. do the permissions vary between the excutions .. i.e. do you execute one with sudo/root privileges, the other as user (unlikely, but thought I'd ask since we don't know what the exact error is)
只是涵盖所有基础.. 执行之间的权限是否有所不同.. 即您是否以 sudo/root 权限执行一个,另一个以用户身份执行(不太可能,但我想我会问,因为我们不知道确切的错误是什么是)
回答by Vladimir
I had similar problem. What helped me:
我有类似的问题。什么帮助了我:
I used windows and putty to write script, so I had \r\n at the end of lines. Be sure, you have only \n symbol.
I copied files and the only way it worked for me at script was
cp <source_dir>/fileName <dest_dir>/fileName
whereas at command linecp <source_dir>/fileName <dest_dir>
worked well too.
我使用 windows 和 putty 来编写脚本,所以我在行尾有 \r\n 。请确保,您只有 \n 符号。
我复制了文件,它在脚本中对我有用的唯一方法是
cp <source_dir>/fileName <dest_dir>/fileName
在命令行中也cp <source_dir>/fileName <dest_dir>
运行良好。