Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm using Mathematica Home Edition 9.0.1 on Mac OS X 10.8.2.

I have this script:

#!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script

exportableDate[date_] := 10000 date[[1]] + 100 date[[2]] + date[[3]];

dividendTable = FinancialData[$ScriptCommandLine[[2]], "Dividend", All]; 
    exportableDividendTable = { exportableDate[#[[1]]], #[[2]] }& /@ dividendTable;
    Export[$Output, exportableDividendTable, "CSV"];

When I run it without redirecting stdout, it works:

$ ./dividendsForSymbol INTC
StringForm["Initializing `1` indices ....", "FinancialData"]
19921026,0.00313
19930126,0.00313
19930426,0.00313
19930726,0.00313
19931026,0.00313
19940126,0.00313
...

When I redirect the output to a file, it just exits immediately without producing any output:

$ ./dividendsForSymbol INTC > out
    $ echo $?
    0
    $ ls -l out
-rw-r--r--  1 mayoff  staff  0 Mar  9 17:18 out

Why is it failing when I redirect the output? Or, how can I diagnose the problem?

share|improve this question
this also fails in the same way #!/Applications/Mathematica.app/Contents/MacOS/MathematicaScript -script Export[$Output, 3, "CSV"]; – acl Mar 10 at 0:22
1  
On OS X Lion with Mathematica 8 the output file is created fine. In version 9 the export doesn't work under Lion. So it seems to be specific to Mathematica version 9. – Jens Mar 10 at 0:56

2 Answers

up vote 6 down vote accepted

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 your existing file dividendsForSymbol, replace $ScriptCommandLine[[2]] with commandLineArg[[1]].

Now if you execute

./dividendsForSymbolWrap INTC >out1.m

the output file should contain the data as they were printed to the terminal without the redirect.

share|improve this answer
Thank you. It appears to be sufficient to change MathematicaScript to MathKernel and $ScriptCommandLine[[2]] to $CommandLine[[4]] in my original script, without needing a separate wrapper. – rob mayoff Mar 10 at 3:10
Absolutely. My thinking was that the wrapper would be more portable with fewer changes to the original script, but since you have to change the original anyway, it doesn't really gain anything (except that it allowed me to illustrate how to pass the command line to another script). – Jens Mar 10 at 3:29
Actually I was mistaken. Bad testing on my part! I do need to use a wrapper like the one you gave. – rob mayoff Mar 10 at 5:55

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"], "Dividend", All];
    exportableDividendTable = { exportableDate[#[[1]]], #[[2]] }& /@ dividendTable;
    Export[$Output, exportableDividendTable, "CSV"];
EOF
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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