Tag Info

Hot answers tagged

14

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


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


11

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


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


9

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


8

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

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


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

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!


6

If you look at this #!/usr/local/bin/math8.0.4/MathematicaScript -script $HistoryLength=0; $pwf=FileNameJoin[{NotebookDirectory[],FileBaseName[NotebookFileName[]]}]; $pwf = "test"; $parameterfile=StringJoin[$pwf,".dat"]; Print[$pwf]; Print[$parameterfile]; Print[$InputFileName]; you will see that the Print statements work just fine - What requires the ...


6

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


6

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


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


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

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.


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


3

It works OK for me here on Mac OS X Mountain Lion, Mathematica 9, given a few minor changes: Mathematica Script saved as math-script.math: #!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script Print["loading"] UsingFrontEnd[NotebookEvaluate["/tmp/test-notebook.nb"]] Print["quitting"] Quit[] Mathematica Notebook saved as ...


3

Extending on nixeagle's answer, here's what I've come up with. First of all, the thing I overlooked is the 3rd/5th bullet point when clicking More Information in the help for Input/InputString, it could not be hidden any better: When no front end is used, Input reads from standard input. Well that answers that, the rest was finding out how these two ...


3

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


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


2

Do I understand it correctly that you have several CPU cores in your machine (say, four of them), and you wish to run as many instances of the simulation as the number of cores, to make maximum use of the available computing power? In this case you can just start four separate instances of the Mathematica kernel on the command line. Simply run MathKernel ...


2

You can obtain kernel IDs with either, Kernels[] or ParallelEvaluate[$KernelID]. ids=ParallelEvaluate[$KernelID] {1, 2, 3, 4} kobjs=Kernels[] {KernelObject[1, "local"], KernelObject[2, "local"], KernelObject[3, "local"], KernelObject[4, "local"]} These can then be used with ParallelEvaluate to specify which kernel you wish to run your code ...


2

No, this is not possible. Manipulate only works in a notebook, opened in the Front End, because it relies on Front End functionality. GUIRunModal uses Java to display a GUI, not the Front End. It is not possible to use Manipulate without the Front End (or CDFPlayer).


2

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


2

You print by having a Print[] on the expression you want to print. End all other statements with ;. fooToPrint = bar[...]; Print[fooToPrint]; fooNoPrint = bar[...]; e.g., a = 1 + 2; Print[a]; b = 3 + 4; a = b; Print[a];


1

Running this script (using the command line ./m.m 10): #!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script Print["$CommandLine ",$CommandLine] Print["$ScriptCommandLine ",$ScriptCommandLine] num = ToExpression[$ScriptCommandLine[[2]]]; Print /@ RandomVariate[ MixtureDistribution[ {1, 2}, {NormalDistribution[1, 0.2], ...


1

I found out how to solve this problem in SQL Sever. I used PsExec together with xp_Cmdshell to do the job. You have to install the PsExec in Windows in the system32 directory of the server machine. You can find the installer here. After that, I created this procedure, that can execute a file in a remote server: CREATE PROCEDURE [dbo].[MAT_EXEC_SCRIPT] ...


1

I can't reproduce this exactly on win7 mma 8.0.1. $ScriptCommandLine is empty, while $CommandLine returns {"d:\\Math\\Mathematica\\8.0\\math.exe", "-noprompt", "-script", "./script.m", "a=6", "z=3", "d=423", "c=43", "x=1"} As a workaround have you thought about using environment variables to pass information to your script? You can retrieve the value of an ...



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