Pythonのsubprocess.call関数で空白文字列を使用する

Pythonのsubprocess.call関数で空白文字列を使用する

Unix環境の関数でコマンドを使用してsshリモートスクリプトを呼び出そうとしますが、リモートスクリプトに文字列パラメータ(スペースを含む)を指定する必要があります。ただし、Pythonスクリプトを実行すると、文字列パラメータが正しく解釈されません。subprocess.callPython 2.6.8

これは私のPythonスクリプトです: -

 #!/usr/bin/python

 from subprocess import call

 ret=call(['ssh','user@host','\"/remote/path/to/script/test.sh','\'Hi','There\'\"'])

print ret

ret2=call(['ssh','user@host','/remote/path/to/script/test.sh','Hello'])

print ret2

エラーとともに次の出力が表示され、returncode 127「空の文字列」を含むsshコマンドの場合、2番目のsshコマンドは正常に実行されますreturncode 0

bash: /remote/path/to/script/test.sh 'Hi There': No such file or directory

127

Hello

0

シェルコマンドライン端末で同じシェルコマンドを実行すると、正常に実行されます。

Command:

ssh user@host "/remote/path/to/script/test.sh 'Hi There'";echo $?

Output:

Hi There

0

リモートスクリプトの内容は次のとおりです/remote/path/to/script/test.sh(助けになる場合)。

#!/usr/bin/ksh

echo $1;

ベストアンサー1

作業バージョンを置き換える文字列を引用します。Hello'"Hi There"':

ret = call(['ssh','user@host','/remote/path/to/script/test.sh','"Hi There"'])

おすすめ記事