sparkandshine sparkandshine - 2 months ago 21
Bash Question

How do I make shell scripts work on both Linux and OS X?

I have a snippet of code to compile a tex document.

#!/usr/bin/env bash

filename="thesis"

pdflatex -shell-escape $filename
makeglossaries $filename
bibtex $filename
pdflatex $filename
pdflatex $filename

# for Ubuntu
#xdg-open $filename.pdf

# for mac
open $filename.pdf


How do I make this script work on both Linux and OS X? I think it should be something like,

if is Linux; then
xdg-open $filename.pdf
elif is OS X; then
open $filename.pdf
fi

Answer

The standard way to detect the current platform is to call uname:

uname=$(uname);
case "$uname" in
    (*Linux*) openCmd='xdg-open'; ;;
    (*Darwin*) openCmd='open'; ;;
    (*CYGWIN*) openCmd='cygstart'; ;;
    (*) echo 'error: unsupported platform.'; exit 2; ;;
esac;
"$openCmd" "$filename.pdf";
Comments