Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which shebang should I use for F# scripts in Mac OS X?

Tags:

shebang

f#

I have F# 2.0 installed with Mono, and I'd like to ./ my F# scripts. Which shebang line should I use for Mac OS X? Can this shebang line be generalized for Mac OS X and Linux?

like image 465
mcandre Avatar asked Nov 10 '12 05:11

mcandre


2 Answers

So, in @mcandre's answer, it works because most shells revert to bourne shell when they don't find a shebang.

So having #light which is both valid F# and a comment in bourne shell allows each scripting environment to see what it understands.

The script inside however could be improved. When we execute a script adding --quiet to --exec seems redundant, also we expect all arguments to be passed to the script. A more ideal version would be exec fsharpi --exec $0 $* so all arguments would be passed as if you ran the command explicitly.

#light is not the only # preceded directive in F#. Preprocessor defines also would work and could be intuitive to what is going on. Putting a shell script between #if run_with_bin_sh and #endif would be invisible to f# since run_with_bin_sh wouldn't be defined.

Example fsx:

#if run_with_bin_sh 
  exec fsharpi --exec $0 $*
#endif
printfn "%A" fsi.CommandLineArgs

update: Real shebang support such as #!/usr/bin/env fsharpi --exec has been add into the official Microsoft F# code base. So it should work with future versions of F#.

update 2: #!/usr/bin/env fsharpi --exec works great for mac, but not on linux. Linux needs to be #!/usr/bin/fsharpi --exec the incompatibilty is annoying.

If you want a cross platform fsharp shebang. The following will work.

#!/bin/sh
#if run_with_bin_sh
  exec fsharpi --exec $0 $*
#endif
like image 181
jbtule Avatar answered Sep 22 '22 08:09

jbtule


NOTE fsharpi has been superseded by dotnet fsi. See this answer.


In Mac OS X, the program is fsharpi.

hello.fs:

#light (*
    exec fsharpi --exec $0 --quiet
*)

System.Console.WriteLine "Hello World"
like image 25
mcandre Avatar answered Sep 20 '22 08:09

mcandre