36
$\begingroup$

When I created a Mathematica script, I found that all my lines were commented out (when opened with vi or gedit or any other text editor). So I either need to get rid of the (**) manually or through a bash script before I run my Mathematica script from a unix terminal.

To create my script, I include the shebang #!/usr/local/bin/MathematicaScript -script on the first line and then save my file as a .m package. The resulting file looks something like this:

(* ::Package:: *)

(* ::Input:: *)
(*#!/usr/local/bin/MathematicaScript-script*)

(* ::Input:: *)
(**)
(*f[x_] := x*)

Why are my lines commented? Is there anything I need to do?

$\endgroup$

1 Answer 1

39
$\begingroup$

The reason why all the lines are commented is because you are using the frontend to create your script in input cells and saving it as a .m file (equivalent to choosing "Mathematica Package" in the Save As dialog).

Now to create a Mathematica package file (or .m file), the code needs to be in initialization cells or code cells. Content in any other cell is commented out. So the change you need to make is to select your input cells and choose Cell > Cell Properties > Initialization Cell or enter your code in a code cell (Cmd8 on a mac). If you save this file, you'll find the code present uncommented in the saved file.

$\endgroup$
13
  • $\begingroup$ Oh my! Thats a nuance I was not aware of. Will try it at once. $\endgroup$
    – dearN
    Commented Aug 27, 2012 at 17:57
  • $\begingroup$ What do you know! Your suggestion worked! Thanks. $\endgroup$
    – dearN
    Commented Aug 27, 2012 at 18:02
  • $\begingroup$ @DNA: It's a blessing in that you can leave all sorts of other stuff -- especially text cells, Section, etc., cells -- for purposes of documentation, test examples, etc., in the source .nb notebook, then just save the notebook as a .m package. $\endgroup$
    – murray
    Commented Aug 27, 2012 at 22:15
  • $\begingroup$ @DNA or you could switch over to working in the .m package directly. $\endgroup$
    – Yves Klett
    Commented Aug 28, 2012 at 5:03
  • 2
    $\begingroup$ @DNA Yves meant that you could also use the front end's package editor to create the .m files. You can get to it by going to File > New > Package or if you open a .m file in Mathematica (notice it looks different from your usual front end notebook). This has nothing to do with how the file is run $\endgroup$
    – rm -rf
    Commented Aug 28, 2012 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.