Given that realpath command does not exists on every unix platform, I would like to replace realpath with readlink command in order to make my shell script as cross-platform as possible.
The thing is that realpath has the --relative-to parameter which is necessary in my case:
$ realpath bdd/full --relative-to bdd
full
on the other hand, readlink seems to provide only the main functionality of realpath command:
$ readlink -f bdd/full
/home/user/bdd/full
After reading the man page, I couldn't find any parameter similar to --relative-to.
Have you any idea if readlink supports a --relative-to-like functionality and if it's not, is there any "clean" way to achive such thing using readlink or any other cross-platform command?
UPDATE:
In my case, --relative-to is necessary for logging purposes, in my shell script, the user passes two relative paths, the relative of the base folder and the relative path of the file on which the action will be performed. In order to print a more-human friendly log entry, I want to print the relative path of file from the base folder path.
I'm currently using the following function to achive my goal:
get_relative_path () {
if command -v realpath &> /dev/null
then
realpath --relative-to="${2-$PWD}" "$1"
else
python -c 'import os.path, sys;print os.path.relpath(sys.argv[1],sys.argv[2])' "$1" "${2-$PWD}"
fi
}