-1

On I am on MacOS, but looking for a cross-platform solution that will also work on *nix. Simple question, how can I resolve this path with bash a script?

/Users/Olegzandr/.nvm/versions/node/v7.2.0/bin/../lib/node_modules/suman/cli/cli.sh

This path is made up of two pieces which I have in hand:

/Users/Olegzandr/.nvm/versions/node/v7.2.0/bin

and

../lib/node_modules/suman/cli/cli.sh

what is the best way to get an absolute path that represents the resolution of those two paths?

Note that my Mac has readlink on it, but it doesn't appear to be the same utility as that on *nix machine. Most importantly, my readlink utility does not have the -f option that *nix utility probably does, so I can't seem to use that.

Alexander Mills
  • 1
  • 80
  • 344
  • 642
  • See: [Getting the Current/Present working directory of a Bash script from within the script](http://stackoverflow.com/q/59895/3776858) – Cyrus Nov 25 '16 at 08:20
  • Just tried a few things from there - can you elaborate on what answer in there you think will help me? – Alexander Mills Nov 25 '16 at 08:25
  • 1
    I suppose the first answer can help you in your script: `DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"`. I have no way to test them with MacOS and bash. – Cyrus Nov 25 '16 at 08:30
  • For my purpores, I can get the same value from simply $(dirname "$0"), that's not what I need I don't think – Alexander Mills Nov 25 '16 at 08:30
  • Please add output of `dirname "$0"` from your script and your desired output to your question. – Cyrus Nov 25 '16 at 08:32
  • Given my answer, do you now know what I am looking for, if you didn't already? – Alexander Mills Nov 25 '16 at 09:01
  • 1
    Why does it need "resolved"? `/Users/Olegzandr/.nvm/versions/node/v7.2.0/bin/../lib/node_modules/suman/cli/cli.sh` is a perfectly fine path that refers to the same file as `/Users/Olegzandr/.nvm/versions/node/v7.2.0/lib/node_modules/suman/cli/cli.sh` – chepner Nov 25 '16 at 12:57
  • Idk depends on the program interpreting the path, not all programs will be able to interpret it correctly – Alexander Mills Nov 25 '16 at 20:43

2 Answers2

1

bash is perfectly able to handle such a task. Taking inspiration from the main question comments, you can do something like this :

MYPATH=/Users/Olegzandr/.nvm/versions/node/v7.2.0/bin/../lib/node_modules/suman/cli/cli.sh
RESOLVED="$(cd $(dirname $MYPATH) && pwd)/$(basename $MYPATH)"
  • dirname returns the path without the file name at the end
  • cd changes to the directory we got, and pwd displays the current directory.
  • basename is the opposite of dirname, and returns the name of the file.
  • as all of those instructions were executed in a subshell, we didn't actually cd in our main script

EDIT: I gave this answer using tools i was familiar with. However, I've just taken a look at the readlink utility you mentioned in your question. It seems to work just fine for me with the -m option. Is this flag available on mac ?

readlink -m /home/myusername/Documents/test_dir/dir_1/../dir_2/test.sh
> /home/myusername/Documents/test_dir/dir_2/test.sh
Aserre
  • 4,397
  • 3
  • 30
  • 51
-1

For the moment, I solved it this way

DIRN="/Users/Olegzandr/.nvm/versions/node/v7.2.0/bin"

EXECDIR="../lib/node_modules/suman/cli/cli.sh"

RESOLVED=$(node -e "var path = require('path'); process.stdout.write(path.resolve('$DIRN/$EXECDIR'))");

Frankly, bash is not cool at all. The fact that I have to resort to these kind of shenanigans is ridonkulous.

Alexander Mills
  • 1
  • 80
  • 344
  • 642
  • 2
    You are overcomplicating the issue. `bash` is perfectly able to handle such a task. As @Cyrus pointed out in the main question comments, you can do the following : `MYPATH=/some/stuff/../otherstuff/a.sh; RESOLVED="$(cd $(dirname $MYPATH) && pwd)/$(basename $MYPATH)"` – Aserre Nov 25 '16 at 10:15
  • Let me try your solution. – Alexander Mills Nov 25 '16 at 10:19
  • Alright your idea seems to work, and I see why it does, feel free to add it as an answer – Alexander Mills Nov 25 '16 at 10:24
  • bash is perfectly capable of solving the problem (with four utiltiies => dirname, pwd, cd, and basename) LOL fml – Alexander Mills Nov 25 '16 at 10:27
  • 1
    to be fair, I answered using tools I was familiar with. However, `readlink` seems to work very well with the `-m` flag (have a look at my edited answer). Is this flag available on mac ? – Aserre Nov 25 '16 at 10:43
  • readlink should work, but apparently not all Mac's have it installed out of the box – Alexander Mills Nov 29 '16 at 20:59