Tag Info

Hot answers tagged

17

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


14

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"] ];


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

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


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

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


9

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


9

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


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

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


7

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


7

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


7

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


6

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


5

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


5

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


5

You can simply use ReadList["!cat"] 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.


5

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


5

Here's an ugly hack that works, based directly on this answer. This doesn't work on Windows. First create a LibraryLink function that can set an alternate signal handler: << CCompilerDriver` handlerlib = " #include <signal.h> #include <stdlib.h> #include <stdio.h> #include \"WolframLibrary.h\" // you may want to ...


5

According to the documentation, To suppress the loading of kernel initialization files, use the kernel command-line option -noinit. To specify another file to be used for kernel initialization in addition to init.m, use the kernel command-line option -initfile file, where file is the additional initialization file.


5

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


4

If you need more interactivity, take a look at mash. From the README: This is how "math -script" should work. Namely, the same way that the perl/python/ruby/etc interpreters do: take a mathematica source file as its first argument (or from stdin if no arguments), make all the arguments available to the mathematica code as an array (list) ...


4

I get two kinds of errors (different from yours) on Mma 8.0.4 Mac OSX, depending on the number of arguments: (with the appropriate path for Mac OSX) #!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script Print[$ScriptCommandLine] With 5 arguments, I get error: 14: Bad address and with 7 or more arguments, I get a segfault: ...


4

If you just need to run a block of Mathematica code then this questions seems to be equivalent to, How do I run an operating system command/script from SQL? Using the operating system command line invocation MathKernel -script myFile.m you can call your Mathematica script by whichever mechanism your version of SQL supports for accessing OS commands. Your ...


4

It turns out if you run an exteral process it gets stdin.. img=Import["!python readstdin.py","tiff"]; Print[Dimensions[ImageData[img]]]; where my python script is just this: import sys print sys.stdin.read() seems to work: math -script script.m < file.tiff (* {900,1200,3} *) Edit .. even better..: img=Import["!cat -","tiff"];


4

On Windows there are two kernel executables: MathKernel.exe and math.exe. MathKernel.exe has its own input window, which may show up separately when you start the process from a command window. If you run it in MathLink mode, it may still show as a taskbar button. math.exe runs in a standard Windows command window. If you start it from within an existing ...


4

(* make the task, fires once a minute *) task = CreateScheduledTask[PutAppend[MemoryInUse[], "myMemory.log"], 60]; (* start the task *) StartScheduledTask[task] (* ... do your thingys ... *) (* stop (or remove) the task *) StopScheduledTask[task] RemoveScheduledTask[task]


3

try: math -initfile test.txt 30


3

You get your desired result by invoking the SetDelayed option for your functions. To output an Integer, remove the N[] function. #!/usr/local/bin/MathematicaScript -script SetOptions[$Output, FormatType -> OutputForm]; foo[bar_?IntegerQ, baz_?IntegerQ] := bar; M[g_?IntegerQ] := Sum[foo[i, g], {i, 1, 4}]; Print[M[1]] (*10*)



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