I was writing a makefile for a web interface program that installs the cgi scripts to a (Linux) OS. The makefile works fine, but it needs to be able to install to different directories for different distributions (Fedora, Ubuntu). On Fedora, the scripts need to go to /var/www/cgi-bin and on Ubuntu, I believe it is /usr/lib/www/cgi-bin.
When I run uname -n on Ubuntu, it outputs "ubuntu", but on Fedora, it gives a combination of the username, computer name, and "hostname".
Does anyone have any idea how to determine the distribution in a makefile?
Thanks

Hello iamthesgt!

I'm sure there's some standard way to do this, but I don't know it. There are lots of pre-existing scripts out there that are similar to this one, but here's something I have been using for a while:

#!/bin/bash

# Check for FreeBSD in the uname output
# If it's not FreeBSD, then we move on!
if [ "$(uname -s)" == 'FreeBSD' ]; then
  OS='freebsd'

# Check for a redhat-release file and see if we can
# tell which Red Hat variant it is
elif [ -f "/etc/redhat-release" ]; then
  RHV=$(egrep -o 'Fedora|CentOS|Red.Hat' /etc/redhat-release)
  case $RHV in
    Fedora)  OS='fedora';;
    CentOS)  OS='centos';;
   Red.Hat)  OS='redhat';;
  esac

# Check for debian_version
elif [ -f "/etc/debian_version" ]; then
  OS='debian'

# Check for arch-release
elif [ -f "/etc/arch-release" ]; then
  OS='arch'

# Check for SuSE-release
elif [ -f "/etc/SuSE-release" ]; then
  OS='suse'

fi

# echo the result
echo "$OS"

It probably needs to be updated, and if you want to get more granular (debian vs ubuntu) or go as far as specific versions for each distro, it'll require a bit more than what's here. Hopefully, though, this will get you started.

-G

Works for me (I was using this in a makefile to determine install directories). This is what I had:

$(shell if [ -f "/etc/redhat-release" ]; then installdir="$fedora"; elif [ -f "/etc/debian_version" ]; then installdir="$ubuntu"; fi)

Actually I initially thought this worked, but it doesn't inside the Makefile (actually deleted the entire computer because the variable was not set, but that's another story). Looking around, I found I needed to set the variable to the end result of a statement, otherwise the variable will only work in local scope of the shell command. So now I've got:

fedora="/var/www/cgi-bin/"
ubuntu="/usr/lib/cgi-bin/"

installdir=$(shell `if [ -f "/etc/redhat-release" ]; then "$(fedora)"; elif [ -f "/etc/debian_version" ]; then "$(ubuntu)"; fi`)

However, this gives me the error: "/bin/sh: /usr/lib/cgi-bin: Permission denied" (even when run as super user, which is how the Makefile must be run). It appears the variable is getting set, but for some reason it can't access the directory. When just run as a shell script (slightly modified):

installdir=`if [ -f "/etc/redhat-release" ]; then "$fedora"; elif [ -f "/etc/debian_version" ]; then "$ubuntu"; fi`

I get the error "/usr/lib/cgi-bin: Is a directory"
It appears the commands are setting the variables or evaluating the commands at least, but just not going further. I don't have a clue to why this is not working. Can anyone help?

Computers are dumb; you need to 'verb' the script what to do. You are executing those values; instead, you need to echo them so that the outer assignment can catch the printed value.

I got it actually, I ended up writing an external shell script an invoking it within the makefile. This is the shell script:

#osfinder_app
# This is a bash script written to determine the operating system being used. 
# Furthermore, this allows a value to be passed back to a variable within the Makefile.
# The paths that follow are for executable files.
# Paths for Fedora distros are /var/www/cgi-bin
# Paths for Ubuntu distros are /usr/lib/cig-bin

#!/bin/bash

if [ -f "/etc/redhat-release" ]
then
returnval=$"/var/www/cgi-bin/"
elif [ -f "/etc/debian_version" ]
then
returnval="/usr/lib/cgi-bin/"
else
returnval="ERROR"
fi
echo $returnval

And I invoke it like this (just set a variable with the output of the script):

app_path=$(shell ./osfinder_app)
$(app_path):
	app_path=$(shell ./osfinder_app)

It now properly returns the proper path for the makefile's use. Thanks for everyone's help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.