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

Eliot Miranda eliot.miranda at gmail.com
Fri Jun 20 14:42:17 UTC 2014



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?

> 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