sparkandshine sparkandshine - 1 year ago 87
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

Answer Source

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";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download