need a good way to determine the OS from a BASH script

D. Hugh Redelmeier hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org
Tue Mar 22 15:44:13 UTC 2011


| From: G. Matthew Rice <matt-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org>

| On Tue, Mar 22, 2011 at 11:06 AM, Digimer <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org> wrote:
| >> Ideally I'd like to be able to detect that I'm about to compile on the MAC
| >> inside the Makefile (or at least in the install script).    Any ideas on how
| >> one could reliably detect a Linux OS as opposed to a Mac OS?
| >>
| >> Thanks in advance for your help.
| >
| > Though it's not technically it's purpose; I've had good luck checking
| > /etc/issue for distro name and version info.
| 
| I have to agree here.  /etc/issue has also been the most reliable way
| to detect the linux distro.
| 
| Also, it maybe time to start looking at tools like autoconf.

(I'm talking about C or C++.)

testing for OS version is a horribly fragile technique

Having to configure your code is something to be avoided as much as
possible.  Sometimes that means not exploiting every feature of a
platform.

Autoconf is an giant ugly tool.  It seems to relish handling all
variations rather than avoiding the issues.

#ifdefs make code much harder to test and much harder to understand.

All config stuff like that ought to be very carefully modularized:
#ifdef platform" should appear at most once in the source code if
at all

Feature-selection macros should be invented if necessary to mediate
between platform selection and feature selection.

The compiler's -D flag allows you to move feature selection out of the
source code and into the makefile(s).

I'm not familiar with the SHM stuff.  What does POSIX say?  Often, if
POSIX offers alternatives, it allows programs to test which
alternative applies.


More information about the Legacy mailing list