Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 778
SparkAndShine Avatar asked Sep 06 '16 10:09

SparkAndShine


1 Answers

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";
like image 76
bgoldst Avatar answered Sep 29 '22 05:09

bgoldst