view raw
sparkandshine sparkandshine - 8 months ago 47
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


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


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

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