Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing F# scripts

Tags:

.net

f#

I'm trying to practice my F# by writing small console scripts in F# in place of my usual method of writing shell scripts/batch files. I currently run them using "fsi script.fsx [args]". Is there a way I can associate these fsx files with fsi so that I can run them directly like "script.fsx [args]"?

like image 245
Ying Avatar asked Mar 17 '10 02:03

Ying


5 Answers

Yes it is possible.

There are two keys here:

  1. You need to bind an application to execute to the ".fsx" extension. The exact way to do this may vary a bit between operating system versions (I have only done this in the past by editing the registry directly, so I can't provide a generic solution here); make sure the command to be executed does include the script arguments (as %*), so the shell\open\command entry in the registry for the binding should look like

    "c:\Program Files\FSharp-1.9.9.9\bin\fsi.exe" "@%1" %*

    See Gradbot's response for more details.

  2. You may want to add the extension you want to trigger script execution (".fsx" in this case) to the PATHEXT environment variable, so CMD.exe can find the script even without you specifying the script's extension. A warning: if you are concerned about your system's security, reconsider this step - the fact that VBScripts are registered on a default Windows XP and older systems to provide a similar feature has been exploited in the past by virus writers (I don't know if this is still true on Vista or windows 7).
like image 96
Luc C Avatar answered Nov 12 '22 00:11

Luc C


As there are no answers so far, I'll try to turn my earlier comment into an answer with some more details.

I just tried to create a standard windows association for *.fsx files to open them automatically in fsi.exe and it works with no problems - when you open the fsx file, it runs F# Interactive and automatically evaluates the content of the file. This corresponds to running the command:

> fsi.exe test.fsx

However, I think that it is much easier to learn F# by using F# Interactive from Visual Studio. Even if you don't have a licence, you can use Visual Studio Shell with the free F# plugin. I think SharpDevelop also provides similar integration (but probably not with some limitations).

The easiest way to start is to create a new "F# Script" (File -> New -> File...) and then start writing some code (in an assorted order). Then you can select some part of the code and evaluate it in the F# Interactive window by perssing [Alt] + [Enter]. You'll first need to open the F# Interactive window, which can be done using [Ctrl] + [Alt] + [F] or you can find it in the View -> Other Windows menu item.

I think most of the F# webcasts use F# Interactive in this way, so if you look at some of them, you should get a good idea how to use it. Look for example at How Do I? videos at www.fsharp.net or F# videos at Channel 9.

like image 42
Tomas Petricek Avatar answered Nov 11 '22 23:11

Tomas Petricek


I know this is very old but... Although I complete agree with everyone who suggests doing REPL-based development, if you find you've written a nice reusable script then here is what works well on Linux and MacOS. Put the *.fsx script in your ~/bin folder or wherever and do the usual shebang trick that you do with other scripts:

First make your script

#!/usr/bin/env dotnet fsi
    
printfn $"1 + 1 = {1 + 1}"

make it executable and run it

chmod 755 foo.fsx
$ ./foo.fsx 
1 + 1 = 2
like image 3
jrk Avatar answered Nov 12 '22 00:11

jrk


Searching for "executing F# scripts on ubuntu" makes you land here.

To execute .fsx scripts with Mac/Linux, write fsharpi yourScript.fsx, for windows users write: fsi yourScript.fsx

like image 2
Lubbi Avatar answered Nov 12 '22 00:11

Lubbi


Here's a walk through on setting the association in windows 7.

like image 1
gradbot Avatar answered Nov 12 '22 01:11

gradbot