linux - "cp -r"在 bash 中表现异常吗?

标签 linux bash

<分区>

我正在编写一个 bash 脚本来使用 puppet 客户端引导云服务器,以便我可以创建一个启动镜像,但是我从一个简单的“cp -r”中得到了奇怪的行为,其中我的源代码的内容正在复制目录,但未复制父目录。例如,如果我有/root/puppet/file1,并且我在我的 bash 脚本中发出了 cp -r/root/puppet/opt 命令,而不是获取/opt/puppet/file1,我正在获取/opt/file1。但是,如果我在脚本执行之前回显该命令,然后复制该回显的输出并在命令行上运行它,那么我会按预期获得/opt/puppet/file1。

这是我的目录结构。

mydirectory
    - script.sh
    - assets
        - puppet
            - file1
            - file2
            - file3

这是我脚本中的片段

#!/bin/bash

### VARIABLE INITIALIZATION
BOOTSTRAP_DIR="/opt/bootstrapping"

# Die with an error message
die()
{
    echo "**** BOOTSTRAPPING FAILED ****"
    echo -e "ERROR:  ${2}"
    exit ${1}
}

# check the return code of the previous command and die if != 0
check_errs()
{
    if [ "${1}" -ne "0" ]; then
        die ${1} "${2}"
    fi
}

# Get the path of the directory from which this script is being executed
SCRIPT_PATH="`dirname \"$0\"`"              # relative
check_errs $? "Could not get the relative path of the executing script"
SCRIPT_PATH="`( cd \"$SCRIPT_PATH\" && pwd )`"  # absolutized and normalized
check_errs $? "Could not get the absolute path of the executing script"

### DEPLOY THE BOOTSTRAPPING FILES
if [ -d "${BOOTSTRAP_DIR}" ]; then
    echo "Removing ${BOOTSTRAP_DIR}"
    rm -rf ${BOOTSTRAP_DIR}
    check_errs $? "Could not remove ${BOOTSTRAP_DIR}"
fi

if [ ! -d "${SCRIPT_PATH}/assets/puppet" ]; then
    die 1 "Can not find ${SCRIPT_PATH}/assets/puppet"
fi

if [ ! -f "${SCRIPT_PATH}/assets/rc.local" ]; then
    die 1 "Can not find ${SCRIPT_PATH}/assets/rc.local"
fi

# This command succeeds, but /opt/bootstrapping/puppet does not exist afterwards.
# Instead I get /opt/bootstrapping/files...
echo "Copying ${SCRIPT_PATH}/assets/puppet to ${BOOTSTRAP_DIR}"
cp -r ${SCRIPT_PATH}/assets/puppet ${BOOTSTRAP_DIR}
check_errs $? "Could not copy ${SCRIPT_PATH}/assets/puppet to ${BOOTSTRAP_DIR}"

echo "Copying ${SCRIPT_PATH}/assets/rc.local to /etc/rc.local"
cp ${SCRIPT_PATH}/assets/rc.local /etc/rc.local
check_errs $? "Could not copy ${SCRIPT_PATH}/assets/rc.local to /etc/rc.local"

谁能解释为什么只复制我的源目录的内容?我觉得这很愚蠢,因为我累了所以我没有看到。

编辑

这绝对是我愚蠢的案例。当脚本调用 cp -r/root/assets/puppet/opt/bootstrapping 时,/opt/bootstrapping 不存在,因此 cp 创建/opt/bootstrapping,并将源文件放入新目录.我不认为这个问题对 future 的任何人都有多大值(value)。除非有人反对,否则我想将其删除。

最佳答案

在 OS X 10.7 上,cp 的手册页说:

 -R    If source_file designates a directory, cp copies the directory and the entire subtree connected at that
       point.  If the source_file ends in a /, the contents of the directory are copied rather than the directory
       itself.  This option also causes symbolic links to be copied, rather than indirected through, and for cp to
       create special files rather than copying them as normal files.  Created directories have the same mode as
       the corresponding source directory, unmodified by the process' umask.

请注意,该选项实际上记录为 -R 而不是 -r 但两者都有效。

我在某个目录中尝试了以下内容:

mkdir -p tmp1/tmp2/tmp3
mkdir test
cp -r tmp1 test

它如您所料的那样工作,即 test 现在包含 tmp1 及其子目录。您能否告诉我们有关您正在使用的操作系统的更多详细信息?另外,您是否检查过您的系统是否具有非标准 cp?

关于linux - "cp -r"在 bash 中表现异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11788877/

相关文章:

linux - 比较两个逗号分隔的字符串并列出共同的值

c - 我应该将什么作为结束字符传递给 grep 等系统命令?

linux - 在多个 tsv(制表符分隔)文件中粘贴标题行

linux - vfork() 失败,错误号 513

linux - 复制文件名没有内容的目录结构

linux - 如何更改信息命令的键?

linux - 如何在Shell脚本中编写正则表达式?

bash - 如何在循环中使用此处文档?

arrays - 理解代码({0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1 })

bash - 如何编写 shell 脚本从curl命令输出中打印表格格式