명령 줄에서 명령 줄 Args, stdin, stdout 및 stderr와 함께 Mathematica 프로그램을 호출하십시오.

StackOverflow https://stackoverflow.com/questions/148057

문제

foo.m에 Mathematica 코드가있는 경우 Mathematica를 호출 할 수 있습니다. -noprompt그리고 함께 -initfile foo.m(또는 -run "<<foo.m") 및 명령 줄 인수는 사용할 수 있습니다 $CommandLine (여분의 쓰레기와 함께) 그러나

#!/usr/bin/env MathKernel
x = 2+2;
Print[x];
Print["There were ", Length[ARGV], " args passed in on the command line."];
linesFromStdin = readList[];
etc.

그리고 chmod it 실행 가능하고 실행합니까? 다시 말해, 다른 스크립팅 언어 (Perl, Python, Ruby 등)와 같이 Mathematica를 어떻게 사용합니까?

도움이 되었습니까?

해결책

MASH -Mathematica Scripting Hack-이 작업을 수행합니다.

Mathematica 버전 6이므로 다음 Perl 스크립트는 다음과 같습니다.

http://ai.eecs.umich.edu/people/dreeves/mash/mash.pl

이전 Mathematica 버전의 경우 C 프로그램이 필요합니다.

http://ai.eecs.umich.edu/people/dreeves/mash/pre6

업데이트 : 마지막으로 Mathematica 8은 "-script"명령 줄 옵션과 함께이를 기본적으로 지원합니다.

http://www.wolfram.com/mathematica/new-in-8/mathematica-shell-scripts/

다른 팁

다음은 추가 도우미 스크립트가 필요하지 않은 솔루션입니다. 다음 Shebang을 사용하여 Mathematica 커널을 직접 호출 할 수 있습니다.

#!/bin/sh
exec <"$0" || exit; read; read; exec /usr/local/bin/math -noprompt "$@" | sed '/^$/d'; exit
(* Mathematica code starts here *)
x = 2+2;
Print[x];

Shebang 코드는 스크립트의 첫 두 줄을 건너 뛰고 나머지를 수학적 커널에 표준 입력으로 공급합니다. 그만큼 sed 명령은 커널에 의해 생성 된 빈 줄을 떨어 뜨립니다.

이 해킹은 다재다능하지 않습니다 으깨다. Mathematica 코드가 읽히기 때문입니다 Stdin 당신은 사용할 수 없습니다 Stdin 사용자 입력의 경우, 즉 함수입니다 입력 그리고 입력 스트링 작동하지 않습니다.

노력하다
-initfile 파일 이름
출구 명령을 프로그램에 넣으십시오

~/.profile의 경로 환경 변수에 Mathematica Binaries를 추가한다고 가정합니다.

export PATH=$PATH:/Applications/Mathematica.app/Contents/MacOS

그런 다음이 Shebang 라인을 Mathematica 스크립트에 작성합니다.

#!/usr/bin/env MathKernel -script

이제 스크립트를 찍을 수 있습니다.

$ cat hello.ma
#!/usr/bin/env MathKernel -script

Print["Hello World!"]

$ chmod a+x hello.ma
$ ./hello.ma
"Hello World!"

Mathematica 8.0으로 테스트.

사소한 버그 : Mathematica는 Windows 및 Mac OS X에 인용문이 있지만 Linux는 인쇄를 둘러싸고 있습니다. WTF?

나는 나를 위해 일한 또 다른 해결책을 찾았다.

코드를 .m 파일에 저장 한 다음 다음과 같이 실행합니다. MathKernel -noprompt -run "

이것이 링크입니다. http://bergmanlab.smith.man.ac.uk/?p=38

Mathematica 7

$ cat test.m
#!/bin/bash
MathKernel -noprompt -run < <( cat $0| sed -e '1,4d' )  | sed '1d'
exit 0
### code start Here ... ###
Print["Hello World!"]
X=7
X*5

용법:

$ chmod +x test.m

$ ./test.m
"Hello World!"

7
35
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top