Skip to main content

外部コマンド実行.標準入力を渡す

Python

py
p = subprocess.Popen(['ls', '-l'], stdin=subprocess.PIPE stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate(input=AAA)
  • 引数指定するだけ。シンプルで良い

シェルに任せる?感じであれば下記か。 でも上記のほうがスッキリする気がする。(安全?)

py
subprocess.check_output('echo "%s" | grep a' % AAA, shell = True)

Bash

bash
echo "$AAA" | grep a
bash
echo "\
aaa
bbb
abc
" | grep a

これでもいい

bash
echo <<EOF | grep a
aaa
bbb
abc
EOF