cp命令的奇怪行为

时间:2011-07-04 13:54:18

标签: linux bash unix

我对BASH脚本有一个奇怪的问题,我无法弄清楚。我不知道为什么会这样。我不会发布整个脚本,因为它的长度只是重要的部分。

信息在脚本的评论中。

Script

# replaces %ZONE% placeholder
# 2 params: path, zone in format z3, z4
zonePath() {
  value=`echo ${1} | sed "s/%ZONE%/${2}/"`
  echo $value
}

createInstance() {
  JBOSS_NAME="jboss"
  JBOSS_DIR="/home/jboss"

  # echo of OUTPUT_ZONE_DIR_TMP looks correctly like /home/jboss/z3
  OUTPUT_ZONE_DIR_TMP=`zonePath ${OUTPUT_ZONE_DIR} ${3}`

  if [[ ! -e ${OUTPUT_ZONE_DIR_TMP}/${JBOSS_NAME}/server/${2} ]]; then
    mkdir -p ${OUTPUT_ZONE_DIR_TMP}/${JBOSS_NAME}/server/${2}
    cp -r ${JBOSS_DIR}/${JBOSS_NAME}/server/default/* ${OUTPUT_ZONE_DIR_TMP}/${JBOSS_NAME}/server/${2}
  fi

  # BUT here I get an error of wrong directory because the path is
  # /home/jboss//jboss/server - the z3 string is missing there - WHY???
  cp -r `zonePath ${SOLUTION_APP_PATH} ${3}`/${1} ${OUTPUT_ZONE_DIR_TMP}/${JBOSS_NAME}/server/${2}/deploy
}

# in the script I call the createInstance function for example like this
createInstance "system-long-name" "sys" z3
createInstance "system2-long-name" "sys2" z4

1 个答案:

答案 0 :(得分:3)

将shebang更改为#!/bin/bash -x并查看输出并确保所有内容都正确展开...

详细了解bash debugging here