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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:42:51  来源:igfitidea点击:

cp command won't run if executed from shell script

linuxbashshellcp

提问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

Make sure you really have bash at /bin/bash. I think a batter hash bang is:

确保你真的有 bash 在/bin/bash. 我认为面糊哈希爆炸是:

#!/usr/bin/env bash

This uses the envcommand to locate the bash binary and set the environment.

这使用env命令来定位 bash 二进制文件并设置环境。

回答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:

我们似乎怀疑这个脚本是如何失败的。如果没有错误消息,那么这是一个奇怪的消息。我建议:

  1. On the command line (which works), do a which cp
  2. Whatever the reply, then copy that and use it as the cpin the script (e.g. /bin/cp)
  3. Check the widcard expansion, run your script with bash -x script-nameand see if you get what you expect.
  4. echo $?after the copy in the script - if it is zero then it (thinks it) worked.
  5. Do a ls -ld /var/www/ksite/appfrom your script, maybe someone set a symbolic link?
  6. If it still fails, source the script from the command-line and see if that works . script-name
  7. Double check that the copy did actually fail! (maybe that should be step 1.)
  1. 在命令行(有效)上,执行 which cp
  2. 无论回复如何,然后复制它并将其用作cp脚本中的(例如/bin/cp
  3. 检查通配符扩展,运行你的脚本,bash -x script-name看看你是否得到了你所期望的。
  4. echo $?在脚本中的副本之后 - 如果它为零,那么它(认为它)起作用了。
  5. ls -ld /var/www/ksite/app从你的脚本中做一个,也许有人设置了一个符号链接?
  6. 如果仍然失败,请从命令行获取脚本并查看是否有效 . script-name
  7. 仔细检查副本是否确实失败了!(也许这应该是第 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:

我有类似的问题。什么帮助了我:

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

  2. I copied files and the only way it worked for me at script was cp <source_dir>/fileName <dest_dir>/fileNamewhereas at command line cp <source_dir>/fileName <dest_dir>worked well too.

  1. 我使用 windows 和 putty 来编写脚本,所以我在行尾有 \r\n 。请确保,您只有 \n 符号。

  2. 我复制了文件,它在脚本中对我有用的唯一方法是cp <source_dir>/fileName <dest_dir>/fileName在命令行中也cp <source_dir>/fileName <dest_dir>运行良好。