-1

And: "Why does this particular script have this outcome?"

From Getting the source directory of a Bash script from within, based on some code snippets offered by user l0b0 in his comment on that question, I used the following for a cron job:

DIR=$(pwd)
if [ $CRON == "true" ]; then
  # If the environment variable $CRON is set to true, we're probably in a cron job
  if [ $PWD == "/" ]; then
    # And if the current working directory is root, we're probably in a cron job
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd && echo x)"
    DIR="${DIR%x}"
  fi
fi

However, my directory variable ($DIR) somehow ends up with a newline after it anyway, which breaks the script any time the $DIR variable is used to create a path. Why is the newline there?

Admittedly, I'm not overly familiar with the nuances of bash scripting and command substitution. It is possible I misunderstood the purpose behind l0b0's script.

Community
  • 1
  • 1
Ryan
  • 659
  • 7
  • 13
  • What happens when you change `echo x` to `echo -n`? – Tripp Kinetics Jul 02 '14 at 17:10
  • @TrippKinetics echo -n seems to work, but so does omitting the echo x and subsequent substitution entirely. I'd like to know why the code does what it does. My understanding of the script was that it preserved directory names that _did_ contain newlines, not add them. – Ryan Jul 02 '14 at 17:23

1 Answers1

0

Simple: the suggested code is wrong and always adds a line feed (because pwd always prints one).

The corrected version would be

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && printf "%sx" "$PWD")"
dir=${dir%x}
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • It's probably necessary that `pwd` is used over `$PWD`. Wasn't it? – konsolebox Jul 02 '14 at 17:55
  • @theotherguy Well I'm not sure either but just wonder why most folks from the thread don't bother about using it. I was hoping you have an idea since you gave a different answer. I also don't want to examine their difference. – konsolebox Jul 02 '14 at 18:37
  • @konsolebox Reading the comments, it looks like they were just not aware of `$PWD`. – that other guy Jul 02 '14 at 19:22
  • @theotherguy I actually remember having a case in Cygwin when my project [Shell Script Loader](http://loader.sourceforge.net) was still in its early stages. I was surprised to see how my scripts failed. The reason on it probably was that `pwd` was giving a different output from `$PWD` (both give valid references to a single target, but give different presentations probably as different links). I examined my scripts and some parts of those refer to checking the difference of two methods. Well this might be a rare case for a real Linux system, although I'm not sure about the others. – konsolebox Jul 03 '14 at 02:48
  • As far as I can tell, depending on the system and the implementation of `pwd`, the output of `pwd` and `$PWD` can differ. The difference seems to usually be in whether or not you get a resolved path (no symlinks) or the path the user/script used to get to the file (possibly symlinks in path). I don't know if that's why they used `pwd` over `$PWD` in the other question thread. – Ryan Jul 03 '14 at 06:23