Last active
December 11, 2021 20:05
-
-
Save bittner/5436f3dc011d43ab7551 to your computer and use it in GitHub Desktop.
Cross-OS compatibility with GNU tools (readlink, sed, zcat, zless, zdiff, ...) for Mac OS X towards Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DESCRIPTION OF PROBLEM: Implementations of sed, readlink, zcat, etc. are different on OS X and Linux. | |
# NOTE: Put this on top of your script using sed, readlink, zcat, etc. that should work alike on Mac OS X. | |
# cross-OS compatibility (greadlink, gsed, zcat are GNU implementations for OS X) | |
[[ `uname` == 'Darwin' ]] && { | |
which greadlink gsed gzcat > /dev/null && { | |
unalias readlink sed zcat | |
alias readlink=greadlink sed=gsed zcat=gzcat | |
} || { | |
echo 'ERROR: GNU utils required for Mac. You may use homebrew to install them: brew install coreutils gnu-sed' | |
exit 1 | |
} | |
} | |
# NOTE: Now all uses of `sed`, `readlink`, `zcat`, etc. will refer to their GNU implementation in your script below. | |
# NOTE: In order to use the original implementation for Mac OS X again you'd have to do `unalias ...` (as in line 7 above). |
Note: If you are using this within a script you will also need to add: 'shopt -s expand_aliases' to the script
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As pointed out by bfontaine on StackOverflow you can, alternatively, add
/usr/local/opt/coreutils/libexec/gnubin
to yourPATH
(obviously before/usr/bin
or so), which will make the commands available without theg
prefix -- without the need for this whole script.Thanks for the hint, @bfontaine!