Skip to main content

外部コマンド実行.標準出力を取得

Python

py
p = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
r = p.returncode # 正常 = 0
  • この方式だと stdout, stderr, returncode が別々で取れるのが良い気がする。

下記もある?

py
subprocess.getoutput('ls -l')
  • subprocess.call() とかは エラー時は例外なので注意。 subprocess.Popen では発生しない
py
import os
os.system('ls -l')
import commands
commands.getoutput('ls -l')

PHP

php
$lastLine = exec($cmd, $out, $retcode);

他に

php
system($cmd);
passthrough()
$ret = `$cmd1`;
  • system <=> exec の違いは、コマンドの実行結果が画面表示されるかどうかの違い?

それぞれのコマンド実行結果受け取り方

戻り値第2引数標準出力
system.
passthru.
exec..

Bash

bash
AAA=`ls -l`
AAA=$( ls -l )

$( .. ) の方は bash でしか使えないが、入れ子できて便利

Ruby

rb
`command1`
Kernel.system('command1')
Open3.capture3('command1')
rb
(str_out, str_err, str_status) = Open3.capture3("pygmentize -f html -P 'tabsize=4' -P 'linenos=1' -l #{tag}", :stdin_data => str_in)
rb
IO.popen('/xx/xx/command', 'r+') do |io|
io.puts "xxxxxxxxxx"
result = io.gets
end

(1) 呼び出して、決まった文字列が来たら落とす

rb
require 'thread'

q = Queue.new

t_command = Thread::new {
IO.popen('コマンド名 引数1 ..') {|io|
while (line = io.gets)
q.push(line)
end
}
}
t_command.run

while (line = q.pop)
if (line ~= /end_end_end/)
t_command.kill
break
end

puts " --- #{line}"
end

(2) 呼び出して、決まった時間経ったら落とす

rb
require 'thread'

lines = Array.new

t_command = Thread::new {
IO.popen('コマンド名 引数1 ..') {|io|
while (line = io.gets)
lines.push(line)
end
}
}
t_command.run

t_timer = Thread::new {
Thread.stop
sleep 5
t_command.kill
}
t_timer.run
t_timer.join

puts lines

C++

cpp
system()
// 外部コマンド実行したいだけのときには使わないのがいい
// 中で fork() するのでメモリを浪費する+時間がかかる?