たちは、それぞれ最初の引数としてファイルパスをスクリプトのカップルを受け入れたとしPythonの場合は、subprocess.check_output
:
で呼び出すことができます
#/usr/bin/env python2
import os.path
import sys
from subprocess import check_output, STDOUT, CalledProcessError
if len(sys.argv) < 2:
sys.stderr.write("Usage: %s input-file" % sys.argv[0])
sys.exit(1)
input_file = sys.argv[1]
if not os.path.isfile(input_file):
sys.stderr.write("%s is not a file" % input_file)
sys.exit(1)
try:
output = check_output(['php', './script.php', input_file], stderr=STDOUT)
print "PHP: %s" % output
output = check_output(['perl', './script.pl', input_file], stderr=STDOUT)
print "Perl: %s" % output
except CalledProcessError as e:
print >> sys.stderr, "Execution failed: ", e
シェルスクリプトにコマンドをラップすることができます。例えば、Bash
スクリプトは次のようになります。
#!/bin/bash -
if ! php ./script.php "[email protected]" ; then
echo >&2 "php command failed"
fi
if ! perl ./script.pl "[email protected]" ; then
echo >&2 "perl command failed"
fi
[email protected]
変数は、スクリプトに渡されたすべてのコマンドライン引数を表します。 if
ステートメントは、コマンドが正常に終了したかどうかを確認します。 echo >&2
コマンドは、標準エラー記述子に文字列を出力します。シェルラッパーを使用すると、Pythonで単一のサブプロセスを呼び出すことができます。
try:
output = check_output(['./call-scripts.sh', input_file])
print output
except CalledProcessError as e:
print >> sys.stderr, "Execution failed: ", e
シェルスクリプトを作成しようとしましたか? –
私はshについて知っていますが、どのようにシェルスクリプトを使用しようとしましたか? – robert
です。たとえば、GNU/Linuxプラットフォームでは、ベストシェルの1つが[Bash](https://www.gnu.org/software/bash/)です。最も単純なケースでは、シェルスクリプトはコマンドのリストのように見えます。 Pythonスクリプトからスクリプトを呼び出す場合は、おそらく['subprocess'](https://docs.python.org/2/library/subprocess.html)を使うべきです。 –