[OT] Shell scripting compatibility

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jul 5 07:59:53 PDT 2009


Jacob Carlborg wrote:
> On 7/5/09 6:36 AM, Nick Sabalausky wrote:
>> "Adam D. Ruppe"<destructionator at gmail.com>  wrote in message
>> news:mailman.5.1246741099.14071.digitalmars-d at puremagic.com...
>>> On Sat, Jul 04, 2009 at 04:50:22PM -0400, Nick Sabalausky wrote:
>>>> Can /bin/bash safely be expected to exist on all non-Windows systems 
>>>> that
>>>> can compile D? Or is there something better for that? Any common
>>>> cross-platform-scripting gotcha's to be aware of?
>>> I know on FreeBSD it is often in /usr/local/bin/bash (if it is installed
>>> at
>>> all), so you can't really rely on it being at /bin/bash.
>>>
>>> I don't really have a solution to cover the differences though. I guess
>>> you could write simple, minimal scripts and just use /bin/sh for best
>>> cross platform luck.
>>>
>>
>> That may work just fine in my case. I'm just using a batch-file (win) 
>> and a
>> shell script (non-win) to launch rebuild to compile the real 
>> cross-platform,
>> umm "script", that's written in D. And even then, only for the cases 
>> where
>> the included precompiled versions are insufficient, such as on a 
>> non-x86 or
>> a mac (my only mac isn't current anymore. and it's completely dead.). 
>> So it
>> is fairly trivial script.
>>
>> So sh is typically in that location then? I know Unix doesn't really 
>> have a
>> way (at least to my knowledge) to handle a script needing a particular
>> interpreter that could be in different places on different machines 
>> without
>> requiring the user to make a symlink or something. But I don't really 
>> need
>> perfect. Good enough is good enough here :)
>>
>>
> 
> #!/usr/bin/env bash
> 
> but that requires env, I don't know if that's any better.

env is on all Unix platforms I tried, just sometimes it's in /bin/ and 
some other times it's in /usr/bin. I know of no portable way to search 
the path and run a program from the shebang on Unix. Second, you can't 
pass options to a program, for example:

#!/usr/bin/rdmd --chatty -O

won't work because the shell will pass "--chatty -O" as one single 
option to rdmd. I worked around that by requiring --shebang if you want 
to pass multiple parameters from within the shebang line:

#!/usr/bin/rdmd --shebang --chatty -O

Then rdmd detects that the argument "--shebang --chatty -O" starts with 
--shebang and therefore splits the rest and parses them as flags.

Very annoying.

By the way, I recently changed rdmd to... but let me make a different post.


Andrei



More information about the Digitalmars-d mailing list