Solved A very simple shell script which tells me if i'm running FreeBSD or Gentoo.

Some ports in FreeBSD & Gentoo have other include directories.
Can I write something "general" a script which detects if it's FreeBSD or Gentoo and then includes the correct directories ?
 
Start with uname, that tells you the name of the OS, the specific version, and the name of the hardware platform (i386 versus ARM for example). It is standardized and available on all Unixes I know of.

On Linux, uname only gives you the version number of the kernel. To find out whether it is RedHat / Debian / Gentoo / ... there is a handful files that are always stored in well known locations you can check, for example /etc/redhat-version, /etc/debian_version, and so on (and honestly, I don't remember whether that's spelled redhat-version, redhat.version or redhat_version).

I don't know of a standardized way (like POSIX or SUS) to identify Linux distributions, other than checking for the presence of these files.
 
I don't have a Gentoo system to test, so I'm extrapolating...

My initial reaction was to suggest uname -v.

But FreeBSD has /etc/os-release, as do all Linux systems running systemd -- which is optional on Gentoo.

I would try something like:
Code:
# case $(grep ^NAME /etc/os-release) in    # option 1
case $(uname -v) in    # option 2
    *FreeBSD*)
        # Set FreeBSD directory list here
        ;;
    *Gentoo*)
        # Set Gentoo directory list here
        ;;
    *)
        echo "Belch!  Are you using Windows?" 1>&2
        exit 1
        ;;
esac
Not sure if "Gentoo" should be capitalised...
 
gentoo has os_release with identifiers as Gentoo in it, generated by their baselayot. usually it is /etc/os-release. More specific check would be "portage" gid and uid both group and user exists on any Gentoo system and used by portage package manager, my recollection it is portage::250 for group
 
I'll ended with the following

Code:
#!/usr/local/bin/zsh 
export f=`cat /etc/os-release | grep FreeBSD` 
export g=`cat /etc/os-release | grep Gentoo` 
echo "f:" $f 
echo "g:" $g 
if [ "" = "$f" ]; then 
    echo "Non-FreeBSD" 
else 
    echo "FreeBSD" 
    ldc2 --gcc=gcc12 -I/usr/local/include/d/gtkd-3 -L-lgtkd-3 -L-ldl hello.d 
fi 

if [ "" = "$g" ]; then 
    echo "Non-Gentoo" 
else 
   echo "Gentoo" 
   ldc2              -I/usr/include/dlang/gtkd-3 -L-lgtkd-3 -L-ldl hello.d 
 
fi
 
Something I often see on internet, is to detect the package manager, it's easy an convenient if you already know what are the OS you're dealing with.
So you can search for 'pkg' and if you don't find it then it's an Gentoo OS otherwise it's a Freebsd OS.

Bash:
#!/bin/sh
if command -v /usr/sbin/pkg >/dev/null
then printf "%s\n" "it's a freebsd installation"
else printf "%s\n" "it's something else"
fi
 
Back
Top