We’re rewarding the question askers & reputations are being recalculated! Read more.
34

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 ...


21

These two small programs -- math.exe is the same as wolfram.exe, and MathKernel.exe is the same as WolframKernel.exe -- are kernel loaders, which provide an interface to the same main kernel code residing in the dynamic library WolframEngine.dll (also known as mathdll.dll in versions prior to 10.1.0). Both accept the same command line options as documented ...


20

Another option is to set FormatType -> OutputForm on the $Output stream: SetOptions[ $Output, FormatType -> OutputForm ]; Print["Hello"]; Or call OutputForm on the string itself: Print[ OutputForm["Hello"] ];


15

The command Run will do exactly that. For example, try Run["touch ~/Desktop/blankfile"] If you want to read the results back in, there are a few options, and the choice between them depends on exactly what you want to do. The RunThrough command lets you read the output of a command-line back as a Mathematica expression. For example, try RunThrough["bc", ...


14

(Reposting my comment as an answer) The reason this is happening is that the Mathematica launcher script sets LD_LIBRARY_PATH so that libraries included in the layout will be found and used, and that setting is inherited by any external process started from the kernel. But, sometimes it may happen that the external executable is linked against a conflicting ...


13

You can use the global AutoOpenNotebooks setting to give a list of notebooks that must be opened on startup. The default path where it looks for these notebooks is $UserBasedirectory/SystemFiles/FrontEnd/TextResources. Now every time you open Mathematica, that notebook will be opened (in my case, tile.nb).


12

WriteString is the function you're looking for. It takes two parameters, the first one being the stream you want to write to (in your case standard output, $Output), the second argument is what you want to print. #!/usr/local/bin/MathematicaScript -script WriteString[$Output, "Hello World!\n"] david@thinkpad:~/temp$ ./asdf Hello World! If you need more ...


12

I have automated a nightly backup process with Mathematica. I use a task scheduling program to run a longer version of the following .m file below with MathKernel. Functions like Complementmake it easy to copy new files from a working directory to a backup directory, and in general, Mathematica makes it easy to use sophisticated logic along with various ...


12

I'm not sure on which operating system you are and whether this makes a difference, but your 4 choice don't do the same. math -run file.m edit: When you change this command to math -run < file.m then it does the same as the next (wrong) alternative. Doesn't do anything with the content of file.m at all, because the -run option expects a command like ...


12

The -script mode is an interface to the wolfram.exe's -script mode, which is a command-line version of Get. It essentially rewrites the command line, launches a kernel using the equivalent command, and then exits. It can only ever be used with a local kernel, and $ScriptInputString will always be None. In -file mode, wolframscript.exe launches a slave ...


11

I think this is a bug in the script execution under version 9. You can work around it by using a wrapper for your script, which loads it using the -run option. For example, save the following as an executable file: /Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run "commandLineArg={\"$1\"}; <<dividendsForSymbol; Exit[]" Then in ...


11

Solution (tested on Linux) Use this as first line of your script: #!/usr/local/bin/MathematicaScript -runfirst "$TopDirectory=\"/usr/local/Wolfram/Mathematica/8.0\"" -script If you installed Mathematica in a different directory, you have to adjust the path of $TopDirectory. How did I debug this? The first error message is quite clear: the system cannot ...


10

Standard input Try using the Input and or InputString commands to read from the standard input. For example a program that does Print[InputString[]]; when run on the commandline with $> echo "Hello" | mathematicaScript Hello Of course this also works from the standard Mathematica workbook. From Invoked program Use Import with a "!" before the ...


10

You can force the Column to display correctly in text-only script mode by passing it explicitly to OutputForm. For example: #!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script list = {a, b, c}; Print[Column[list] // OutputForm]; gives the output you expect: a b c


10

On Windows, MathKernel.exe launches the kernel running within a window. The standard output is redirected to that window. To avoid this redirection, we must run the kernel as a console application using Math.exe instead: "C:\Program Files\Wolfram Research\Mathematica\10.0\Math" ^ -noprompt ^ -script C:\Users\a\Desktop\test.m ^ "data goes here" ...


10

It is not a good idea to try to run a notebook in command line mode. Whether or not it is possible, it is just not a good idea because notebooks can only be handled by the Front End, which is not a command line tool. If you go this route anyway, expect difficulties. The usual way is to extract the code you want to run into a plain text .m file and run ...


10

Here is an idiom used by many built-in functions to achieve this effect: doubleOurMoney::badarg = "`1` should be of the form money[number]"; doubleOurMoney[money[x_?NumericQ]] := money[2 x]; doubleOurMoney[args___] := ( Message[doubleOurMoney::badarg, args]; Null /; False ) Here I'm using _?NumericQ instead of _Real to make your function work on ...


9

What I do is the following. Save the following code as a text file in a permanent location under the name MathematicaLauncher.scpt: tell application "System Events" try get process "Mathematica" on error -- Not running, launch and run launch application "Mathematica" -- May need to wait until application finishes launching ...


9

You can simply use ReadList["!program"] possible specifying an input type (Number, String, etc.) in ReadList. While Read reads only until a newline, ReadList will read until an EOF character. I have tested this on Windows (with math.exe -script) using the GnuWin32 version of cat and it worked.


9

This turns out to be easy. In Windows 7: Add C:\Program Files\Wolfram Research\Mathematica\9.0\ to the path. Write procedures for what you want to do in The Wolfram Language, and save in a .m file. Add MathKernel.exe -script filename.m to the Windows Task Scheduler. Specify that the job runs in the directory containing the .m file. That seems to be it. by ...


8

It is indeed possible and I use this all the time. Instead of the for ... & done you might want to check out gnu parallel. It's great for running parallel processes from the command line. parallel can be told how many processes to run in parallel, so you don't run out of licences or memory. Here is an example: parallel -j4 "math -noprompt -script ...


8

It is possible, and I do it routinely. The limitation is the number of kernels your license allows you to run simultaneously. This I believe is reported by $MaxLicenseProcesses: Of course you need to be careful not to write to the same files etc!


8

Generally, it is better to use softer than harder error-reporting mechanisms. Calling Quit[] seems too radical to me. What normally happens is that functions where the error occur are not in the position to make right decisions about what to do with the error, since they are likely too low-level. This is exactly the reason why exceptions exist. They (...


8

Here is a minimal example on how to run a mathematica script on a raspberry pi. Create a simple script (I used my bash terminal to do it): echo "sum=1+1; Print[sum]" > test.wl Excecute the script: wolfram -script test.wl 2


8

Update a very simple solution to the OP has been given in this very duplicate post: all the outputs (in particular graphics) are then correctly inserted in the evaluated notebook without the need to insert/modify the original notebook with special commands. also, as asked in the comments, I added a way to monitor in real time the different kind of outputs ...


8

To replicate the Ctrl+A Enter execution behaviour automatically you can run Solve.nb from a package file, say runSolve.m, containing the code below. While Wolfram Language Scripts are straightforward, this method has the advantage that output can be saved in the notebook as if it was being run manually. To run a notebook and monitor evaluation ...


8

I didn't check how formatting works in wolframscript but if you force it on your terms then it does not matter: TL;DR: use: $ wolframscript -cloud -code 'ToString @ TeXForm[ (d u)/(d t)]'


7

SeedRandom[1234567]; q = RandomReal[{-1, 1}, 6]; r0 = RandomReal[{-3, 3}, {6, 2}] q = q - Total[q]/6; phi = Sum[ q[[i]]/Sqrt[({x, y} - r0[[i]]).({x, y} - r0[[i]])], {i, 1, 6}]; f = -Grad[phi, {x, y}] Show[StreamPlot[Evaluate[f], {x, -4, 4}, {y, -4, 4}, StreamColorFunction -> "Rainbow", StreamColorFunctionScaling -> False], Graphics[Table[ ...


6

I stumbled on this bug today, on a linux box. Same symptoms, redirection of the stdout from an executable script, thescript, that begins with #!/usr/bin/env MathematicaScript -script (* math stuff *) when invoked as $ ./thescript arg1 > somefile or $ ./thescript arg1 | head resulted in an empty somefile and nothing from head. But without piping or ...


6

In the end, I just changed my script to a bash script that feeds a here-document to MathKernel: #!/bin/bash export symbol="$1" /Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt <<\EOF | grep -v ^StringForm exportableDate[date_] := 10000 date[[1]] + 100 date[[2]] + date[[3]]; dividendTable = FinancialData[Environment["symbol"],...


Only top voted, non community-wiki answers of a minimum length are eligible