Shell scripts occasionally hardcode paths to other files. This will break if these other files are relocated. Hence, you should refer to outside files always relative to the position of the shell script itself. This can be achieved with the following code:

HERE=$(dirname $(readlink -f "${0}"))
echo "${HERE}"

This will always return the absolute path to the directory the script is located in, regardless of how it was called. For example, if the script returndir.sh is currently located in /home/aaron/Documents, no matter from where you call it, it must always return /home/aaron/Documents, and when I move it to somewhere else, the return should change accordingly.

Suppose now you are working on a server where no terminal is available, but alternatively you have wolfram script engine installed on that machine, how do you write a wolfram script to perform the same task?

At first I thought it was quite easy, here is my version of returndir.wl:

#!/usr/local/bin/MathematicaScript -script

After some test, this code was proved wrong: it gives you the directory from where you call it, not where the script file is located. Does there exist some way to solve the problem?



Browse other questions tagged or ask your own question.