mathematica script run in linux command line - wolfram-mathematica

Suppose we want to integrate e**x in linux command line and every time the integration range may vary.We can call the integration script and input the specified integration range every time in command line.
I have read http://reference.wolfram.com/mathematica/tutorial/MathematicaScripts.html,but it is sort of terse.
Q1
Is it a .m script necessarily?
Q2
Could it be a nb file?Then the formula could be written easily.

for something this simple you dont even need a script:
echo "Integrate[Exp[x],{x,0,y}]" | math
If you want to batch execute notebooks you will need to create a wrapper .m script that opens and evaluates the notebook -- Way overkill for this problem but see here:
mathematica start front end and eval notebook from command line

Related

Call UniVerse Command from Shell

I have a UniVerse (Rocket U2) system, and want to be able to call certain UniVerse/TCL commands from a shell script. However whenever I run the uv binary it seems to stop the execution of the rest of the shell script.
For Example if I run:
/u2/uv/bin/uv
It starts a UniVerse session. The next line of the script (RUNPY run_tests.py) is meant to be executed in the TCL environment, but is never input to TCL. I have tried passing in string parameters to the uv binary to be executed, but doesn't appear to do anything.
Is there a way to call UniVerse/TCL commands from a UNIX/Shell environment?
You can type this manually or put it into a shell script. I have not run into any issues with this paradigm, but your choice of shell could theoretically affect this. You certainly want to either be in the directory of the account you want execute it in or cd to it in the script.
/u2/uv/bin/uv <<start
RUNPY run_tests.py
start
Good Luck.
One thing to watch out for is if you have a LOGIN paragraph or something else that runs automatically to start your application (which is really common), then you need to find a way to bypass this for non-interactive users.
https://groups.google.com/forum/#!topic/comp.databases.pick/B2hzuXq3X9A mentions
IF OCONV(#TTY,'MCU')='PHANTOM' THEN ABORT
In UD, I kick off scripts from unix as a phantom to a) capture the log output in PH and b) end the process if extra input is requested, rather than hanging around. In UD that's
$echo "PHANTOM COUNT VOC" | udt
UniData Release 8.1 Build: (2008)
Current UniData home is /unidata/ud81/.
Current working directory is /usr/ud81/demo
:PHANTOM COUNT VOC
PHANTOM process 18743448 started.
COMO file is '_PH_/dsiroot45172_18743448'.
:
Critical abort condition found.
$cat _PH_/dsiroot45172_18743448
COUNT VOC
14670 record(s) counted.
PHANTOM process 18743448 has completed.
Van Amburg's answer is the most correct for handling multiple lines of input. The variant I used was instead of the << command for multi-line strings I just added quotes around a single command (single and double quotes both work):
/u2/uv/bin/uv "RUNPY run_tests.py"

How to use variables from a Bash shell script in Matlab m-file

I've inherited an m-file that calls the function:
function conditions(varargin)
This requires me to type a series of names when running the m-file. E.g.,:
conditions('c01','c02','c03')
I also have a text file (conditions.txt) that contains all conditions names, one conditions per line. e.g.,:
'c01'
'c02'
'c03'
etc...
Is there a way to automate conditions.m through the Bash shell by running through each line of the text file, one line at a time?
Since you're happy to take other suggestions as well, I'm going to suggest you do this through a simple MATLAB script. MATLAB takes a while to start up, so it is quite inefficient to start MATLAB anew for each "condition".
I'm going to assume that your text file example is a simplification, maybe you have multiple parameters per line, and need to pass each of them as an argument to the conditions function. For example the file conditions.txt could contain:
'c01',5,false
'c02',100,true,0,'foo'
...
and you'd want to generate the calls
conditions('c01',5,false)
conditions('c02',100,true,0,'foo')
...
The following code accomplishes this:
f = fopen('conditions.txt','rt');
while true
data = fgetl(f);
if isequal(data,-1), break, end
eval('conditions(',data,')')
end
fclose(f);
exit
You could save that as an M-file script (e.g. runconditions.m), and execute it from bash as follows:
matlab -nosplash -nodesktop -r runconditions
eval is evil, in general, but in this case it's the same as what you'd be doing from the Bash script in Christian's answer. The only difference with that answer is that you avoid starting up MATLAB repeatedly.
At least on windows you can call matlab functions from batch like this: matlab -wait -r 'conditions(%var%)'
So your bash looping should be solvable with a code snipped like this:
while read p; do
matlab -wait -r 'conditions($p)'
done <yourfile.txt
Or any other looping style of your choice:
Looping through the content of a file in Bash

Bash Script: Wait Until Google Compute Job Done?

I have a bunch of bash scripts that I run sequentially. I'm going to consolidate to a single script but there's one part that's a bit tricky. Specifically, script C launches a Google Compute Engine job and I only want script D (the one immediately following it) to execute once that's done.
Is there a good way of doing this?
In case it helps, my new script would be:
source script_A.sh
source script_B.sh
source script_C.sh
**wait until cloud job has finished**
source script_D.sh
Thanks!
After gcloud ... & is called, use gcloudpid=$! (I don't think you have to export, but it wouldn't hurt) to grab its pid. then your main script will be
source script_C.sh
wait $gcloudpid
source script_D.sh

How do I write a shell script that repeats a java program a specific number of times?

Essentially I am looking to write a shell script, likely using a for loop, that would allow me to repeat a program call multiple times without having to do it by hand (I don't know exactly how to explain this, but i want to perform the java TestFile.java command in the cmd window multiple times without doing it by hand).
I am trying to write it for the UNIX shell in bash, if that helps at all.
My program outputs a set of numbers that I want to look at to analyze end behavior, so I need to perform many tests for many different inputs and I want to streamline the process. I have a pretty basic understanding of shell scripting - i tried to teach myself today but I couldn't really understand the syntax of the for loop or the syntax of how to write a .java file call, but I would be able to write them in shell script with a little help.
This will do:
#!/bin/bash
javac Testfile.java # compile the program
for((i=1;i<=50;i++))
do
echo "Output of Iteration $i" >> outfile
java Testfile >> outfile
done
This will compile your java program and run it for 50 times and store the output in a file named outfile. Likewise, you can change the 50 for the number of iterations you want.
#!/bin/bash
for i in {1..10}
do
#insert file run command here
done
#!/bin/bash
LOOPS=50
for i IN {1 .. LOOPS}
do
java TestFile >> out.log
done

R Script: Determine whether the script is run in the GUI or from command line

Is it possible to determine - from within the script - whether the script is running in the R-GUI (specifically R.app on OS X) or whether it has been called from Terminal/command line (i.e. R --vanilla -f script.R)? If so, how is this possible?
I'm asking because I have a script that can run parallelized (using the doMC library), which should not be used from the GUI. Sometimes I need to further process the data calculated in the script, so I'd like to call the script from the GUI on these occasions.
Perhaps you are looking for interactive()?
In C, you would use the isatty function. If you could find an equivalent function in R (probably in a UNIX or file system library), that should help.

Resources