Comment detail

指定コマンドを別プロセスで起動 (Nested Flatten)
Cygwinシェル上でmath.exeを実行した結果

In[1]:= Run["ps"]
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
     4876       1    4876       4876  con 1003 15:38:24 /usr/bin/bash
     1348    4876    1348        736  con 1003 17:16:08 /cygdrive/c/Program Files/Wolfram Research/Mathematica/5.2/math
     2368       1    2368       2368  con 1003 17:16:16 /usr/bin/ps
Out[1]= 0

これではリターンコードは戻り値として取得できるが、出力は表示されるだけで再利用できない。

仕方なく、出力を一度ファイルに入れることにする(Windows以外ならもっと簡単なはず)。
結果リストの1番目がリターンコード、2番目が出力

In[3]:= exec@"ps"

Out[3]= {0, {      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND,
>          4876       1    4876       4876  con 1003 15:38:24 /usr/bin/bash,
>          1348    4876    1348        736  con 1003 17:16:08\
>      /cygdrive/c/Program Files/Wolfram Research/Mathematica/5.2/math,
>          4768       1    4768       4768  con 1003 17:17:33 /usr/bin/ps}}
1
2
3
4
5
6
7
8
exec[command_] := Module[{
    code,result,
    file = First@ReadList["!mktemp",String]
  },
  code = Run[command <> " > " <> file];
  result = ReadList[file,String];
  Run["rm "<>file];
  {code,result}]

Index

Feed

Other

Link

Pathtraq

loading...