1

I have a strange problem with BASH script which I can't figure out. I don't know why this keeps happening. I don't post the whole script because of its length but only the important parts.

Information is in the script's comments.

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
user219882
  • 15,274
  • 23
  • 93
  • 138
  • 4
    as always, change the shebang to `#!/bin/bash -x` and look at the output and make sure that everything is expanded properly... – Fredrik Pihl Jul 04 '11 at 14:00
  • @Fredrik Thanks, I didn't know how to debug it. I found a little mistake there... – user219882 Jul 04 '11 at 14:08
  • Instead of testing -e for a directory, you would better test -d – hornetbzz Jul 05 '11 at 07:53
  • Also path does not need to be quoted in bash, and you could try w/o substituting JBOSS_NAME, just using "$JBOSS_NAME" to preserve white space in case. – hornetbzz Jul 05 '11 at 08:09
  • @hornetbzz What's the difference between -d and -e? And what did you mean by the "w/o substituting JBOSS_NAME"? – user219882 Jul 05 '11 at 10:45
  • In Linux, all is a file but you can better check whether this is an existing directory rather than checking this is an existing file (which could also be a directory). Pls see [this thread](http://stackoverflow.com/questions/59838/how-to-check-if-a-directory-exists-in-a-bash-shell-script) – hornetbzz Jul 05 '11 at 10:56
  • then about substitution, I see no point to using ${JBOSS_NAME} in your path. You could just use $JBOSS_NAME between double quotes, then echo the full path and check the result before running any cp commands. – hornetbzz Jul 05 '11 at 10:59
  • In longer functions you might want to consider using `f () { local arg1="$1" arg2="$2";}`. That documents the interface as well. – Coroos Jul 23 '13 at 08:43

1 Answers1

3

Change the shebang to #!/bin/bash -x and look at the output and make sure that everything is expanded properly...

Read more about bash debugging here

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 1
    also good to mention: `-e` to stop on errors (under normal signal trapping) – sehe Jul 04 '11 at 15:01
  • Tomas, the answer was accepted, but it doesn't have to do with the real question (a `cp` problem). What was the `cp` error, anyway? How was that solved? – mgarciaisaia Dec 07 '12 at 15:19