https://github.com/hugojosefson/realpath-compat
An implementation of realpath(1) that should work everywhere.
https://github.com/hugojosefson/realpath-compat
bash ksh linux mac macos portable readlink readlink-f realpath sh shell zsh
Last synced: 2 months ago
JSON representation
An implementation of realpath(1) that should work everywhere.
- Host: GitHub
- URL: https://github.com/hugojosefson/realpath-compat
- Owner: hugojosefson
- Created: 2022-09-05T08:24:57.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-05T08:43:51.000Z (almost 4 years ago)
- Last Synced: 2025-01-31T17:48:15.859Z (over 1 year ago)
- Topics: bash, ksh, linux, mac, macos, portable, readlink, readlink-f, realpath, sh, shell, zsh
- Language: Shell
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# realpath-compat
An implementation of [realpath(1)](https://linux.die.net/man/1/realpath) that
should work everywhere.
## Installation
Copy this function into your shell script:
```sh
realpath_compat(){ if [ $# -eq 0 ];then echo "Usage: realpath_compat FILE [FILE ...]">&2;return 1;fi;while [ $# -gt 0 ];do file="$1";shift;if ! [ -e "$file" ];then echo "realpath_compat: $file: No such file or directory">&2;return 1;fi;realpath "$file" 2>/dev/null||readlink -f "$file" 2>/dev/null||zsh -c 'echo ${0:A}' "$file" 2>/dev/null||python3 -c "import os; import sys; print(os.path.realpath(sys.argv[1]))" "$file" 2>/dev/null||node -e "console.log(require('fs').realpathSync(process.argv[1]))" "$file" 2>/dev/null||echo "realpath_compat: $file: Could not resolve real path">&2;done;}
```
Alternatively, copy the non-minified version found in
[realpath-compat](realpath-compat#L6-L30).
## Usage
Call the function, with some file(s) as argument(s).
Example:
```sh
#!/bin/sh
set -e
realpath_compat(){ if [ $# -eq 0 ];then echo "Usage: realpath_compat FILE [FILE ...]">&2;return 1;fi;while [ $# -gt 0 ];do file="$1";shift;if ! [ -e "$file" ];then echo "realpath_compat: $file: No such file or directory">&2;return 1;fi;realpath "$file" 2>/dev/null||readlink -f "$file" 2>/dev/null||zsh -c 'echo ${0:A}' "$file" 2>/dev/null||python3 -c "import os; import sys; print(os.path.realpath(sys.argv[1]))" "$file" 2>/dev/null||node -e "console.log(require('fs').realpathSync(process.argv[1]))" "$file" 2>/dev/null||echo "realpath_compat: $file: Could not resolve real path">&2;done;}
my_real_path="$(realpath_compat "$0")"
echo "This script's real path is: ${my_real_path}, and that's true even if you call it via a symlink."
```
## License
realpath-compat by Hugo Josefson is marked with CC0 1.0
