MacOS command-line - possible to write portable scripts across MacOS and Linux?

3

1

I've been mostly pleased with MacOS as a dev platform, and it's somewhat similar to a linux command-line, but there are a number of notable exceptions:

  • all commands: arguments parsing behavior differs. Linux will accept flags in any order, including flags following files. In MacOS, flags following files are ignored; flags must be presented before any filenames

  • base64: not installed by default. once installed, does NOT accept the '-i' flag which is required on Linux to ignore whitespace in base64 encoded text.

  • sed: MacOS treats the "-i" flag VERY differently than Linux (requires an extension which breaks scripts on Linux). extended regex is "-E" on MacOS and "-e" on Linux

(plus a variety of other issues...)

In almost every case, I have found the Linux behavioral variant to be easier to use / more powerful. Is there a way to upgrade the MacOS command-line utilities to make it more similar to Linux? Or am I thinking about this wrong and should drink some kool-aide? It's minor, but these glitches have me pulling my hair out when trying to move back and forth between Linux / MacOS dev boxes.

Dave Dopson

Posted 2011-07-14T18:17:26.273

Reputation: 1 215

1You should refer to the OS as "Mac OS X" or just "OS X", and not "MacOS", which is the name that was used 10 years ago, before OS X, and before there was a UNIX foundation to the operating system. OS X is essentially built on NeXT. They built an emulation layer for the old MacOS (called "Classic") but basically no one uses that anymore: it was just for handling the transition from MacOS (Classic) to Mac OS X. Googling will give you a more complete history, if you're interested. – iconoclast – 2011-07-14T19:10:13.937

@Brandon Mac OS X has been the only Mac OS for a decade now, and given the additional context of porting Unix shell scripts, there was absolutely no ambiguity in what he said. Did you really think he might have been referring to MPW? – Spiff – 2011-07-14T19:19:23.297

Answers

2

Darwin, the Unix foundation of Mac OS X, is essentially a flavor of BSD, so some of the differences you're seeing are due to that. The lack of certain tools which might be standard on most or all Linux distros can be addressed by

  • using a package manager, such as
    • MacPorts, or
    • Fink; or by
  • installing from source.

Sometimes packages are unacceptably far behind. For instance, I found a bug in tmux 1.4 that makes it unusable for me, but macports does not have 1.5 yet. So installing from source may be preferable in many cases.

iconoclast

Posted 2011-07-14T18:17:26.273

Reputation: 2 752

Hmm. installing from source is a pain in the ass, but might work... – Dave Dopson – 2011-07-20T20:50:20.963

2

The various GNU tools are available in Fink or DarwinPorts or whatnot.

Ignacio Vazquez-Abrams

Posted 2011-07-14T18:17:26.273

Reputation: 100 516

0

There is a bunch of "same" programs which do not interpret options in one *nix in the same way as they interpret the same options in the other *nix OS, including linux too. There is nothing one can do about it.

And if shell's built-in commands bother you, type in a shell ps -p $$. See the name of the shell output under CMD. If the shells are different, than just install on the OSX the same type of the shell as found in linux. Can be tcsh and zsh.

Karmen

Posted 2011-07-14T18:17:26.273

Reputation: 1

0

1) Install Homebrew

2) "brew install coreutils gnu-sed gnu-tar gnu-getopt gnu-time gnu-which"

Then all the standard linux stuff is available with a "g" prefix -- so "gtar" instead of "tar".

With a bit of hackery, it's possible to flip between the original BSD programs, and the GNU/Linux versions. eg, set up a /usr/local/gnubin that has symlinks like tar --> /usr/local/bin/gtar. Then put that dir first in the PATH variable. There's also using aliases, but I'm not a big fan of aliases that are the same as a real program (I find that to be fragile and error prone).

Dave Dopson

Posted 2011-07-14T18:17:26.273

Reputation: 1 215