为什么"cp"在此shell脚本中失败? [英] Why is 'cp' failing in this shell script?

查看:297
本文介绍了为什么"cp"在此shell脚本中失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我编写的shell脚本的代码段:

Here's a snippet of code from a shell script I have written:

for src in $(find . -type f -maxdepth 1 \! -name ${deploy} \! -name gvimrc)
do
    src=$(basename ${src})
    dest="~/.${src}"
    copy="${src} -> ${dest}"
    cp $src $dest && echo -e "${ok} ${copy}" || echo -e "${fail} ${copy}"
done

由于某些原因, cp 无法执行.例如,在 $ src ='bashrc'的情况下,出现此错误:

For some reason, cp fails to execute. For example, in the case in which $src='bashrc', I get this error:

cp: ~/.bashrc: No such file or directory

我不明白为什么会发生这种情况,因为很明显,〜/.bashrc 目标,而不是源,所以是 cp 不在乎它是否存在.到底出什么问题了?

I don't understand why this happens, though, because obviously, ~/.bashrc is the destination, not the source, so cp shouldn't care whether it exists or not. What exactly is going wrong?

推荐答案

您已经逃脱了〜,因此它不会扩展,并且cp也不知道该怎么做.

You have escaped the ~ and so it won't get expanded and cp doesn't know how to do it.

使用

dest=~/".${src}"

bash应该这样做.

bash should do it.

这篇关于为什么"cp"在此shell脚本中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆