[Vm-dev] conditionally define a function in sh/bash?

Frank Shearar frank.shearar at gmail.com
Fri Jun 20 15:35:26 UTC 2014


On 20 June 2014 15:42, Eliot Miranda <eliot.miranda at gmail.com> wrote:
>
>
>
> On Jun 20, 2014, at 1:14 AM, Frank Shearar <frank.shearar at gmail.com> wrote:
>
>>
>> On 20 June 2014 01:17, Eliot Miranda <eliot.miranda at gmail.com> wrote:
>>>
>>> Hi All,
>>>
>>>    I'm trying
>>>
>>> if [ "$OS" = Darwin ]; then
>>>    function quietmd5 () { /sbin/md5 -q "$1"; }
>>> else
>>>    function quietmd5 () { /usr/bin/md5sum "$1"| sed 's/ .*$//'; }
>>> fi
>>>
>>> but bash no like :-(
>>
>> That's an odd-looking construction- why would you want to
>> conditionally define the function, as opposed to define the function
>> with the conditional inside?
>
> well, squint at it and its almost object-oriented, "here are the implementations of functions on macos; here are the implementations of functions on Linux". And if the use count is high it's more efficient.  And it reads nicer.  You'd be at least mildly offended to see such if-then-else code in Smalltalk right?

I think it reads the same: I mean, it's bash, so by definition
"stinky" is a suitable adjective.

Efficiency's a valid point though: that hadn't occurred to me at all.
It's like a macro (in the lisp sense) that gives you a
platform-specific function.

But I usually think of this quote I saw somewhere: "if it's more than
10 lines of bash rewrite it in a proper language". Sadly, that's not
an acceptable comeback for the launcher: "oh, so to run this general
purpose language I need this other general purpose language installed
already? Why don't I just go use that one?"

frank

>> function quietmd5 () {
>>  if [ "$OS" = Darwin ]; then
>>    /sbin/md5 -q "$1";
>>  else
>>    /usr/bin/md5sum "$1"| sed 's/ .*$//';
>>  fi
>> }
>>
>> ??
>>
>> frank
>>
>>> --
>>> best,
>>> Eliot


More information about the Vm-dev mailing list