CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Detecting the target operating system platform, continued

In addition to detecting operating system versions and characteristics using the C preprocessor, we can do the same with other utilities.

Within Makefiles we can invoke external programs, capture their output into a make variable, and then conditionally execute different commands or apply different command-line options:

OS := $(shell uname)
NAME		= project2
BACKUPTGZ       = $(HOME)/backup/$(NAME).tgz

backup:
        $(MAKE) --no-print-directory prepare
ifeq ($(OS),Darwin)                                     # for macOS
        COPYFILE_DISABLE=1 tar zcf $(BACKUPTGZ) .
else                                                    # assuming Linux  
        tar zcf $(BACKUPTGZ) .
endif

And, unsurprisingly, we can perform the same command sequence within shellscripts:

#!/bin/bash
OS=$(shell uname)
NAME=project2
BACKUPTGZ=$(HOME)/backup/$(NAME).tgz

case "$OS" in
    Darwin)
        echo hello from macOS
        COPYFILE_DISABLE=1 tar zcf $(BACKUPTGZ) .                         
        ;;
    Linux)
        echo hello from Linux
        tar zcf $(BACKUPTGZ) .
        ;;
    *)
        echo 'unsupported platform!'
        exit 1
        ;;
esac

 


CITS2002 Systems Programming, Lecture 22, p6, 16th October 2023.