I have a very simple program that export a list to a data file in table form.

lst = {{1/10, 1/3}, {1/3, Pi}};
lst = N[lst, 10]
Export["testlst.txt", lst, "Table"];

It works fine in the notebook interface. However, I want to execute it in the terminal, so I write it in a lst.m file:

lst = {{1/10, 1/3}, {1/3, Pi}};
lst = N[lst, 10]
(*I need to comment out this line because otherwise there will be error.*)
(*by the way, how to set it to current directory in script interface?*)
Export["testlst.txt", lst, "Table"];

Then I execute it with: MathKernel -script lst.m command. When I open testlst.txt I only found:


in it.


I made a mistake copy it to the script file. Somehow lst = {{1/10, 1/3}, {1/3, Pi}}; changes into t = {{1/10, 1/3}, {1/3, Pi}}; during the copy. No wonder the mistake.

The code works in terminal actually.


I think in terminal, the default output directory is the current directory.


This works for me:

Create try.m file with this in it:

    SetOptions[ $Output, FormatType -> OutputForm ];
lst = {{1/10, 1/3}, {1/3, Pi}};
lst = N[lst, 10]
Export["testlst.txt", lst, "Table"];

Now from DOS window, type

>"C:\Program Files\Wolfram Research\Mathematica\10.3\math.exe" < try.m

Check the file

>type testlst.txt
0.1     0.3333333333
0.3333333333    3.141592654

it worked!

ps. I do not know how to set directory in script to the location of the script file. I always set it explicitly as in the above.

| improve this answer | |
  • $\begingroup$ I actually made a mistake in the code when I copy the code to the script. Somehow lst = {{1/10, 1/3}, {1/3, Pi}}; changes into t = {{1/10, 1/3}, {1/3, Pi}};. No wonder the mistake. $\endgroup$ – an offer can't refuse Dec 28 '15 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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