Shell 脚本以 Python 变量作为参数
Shell script taking for argument a Python variable
我有一个名为 'scriptNastran.py' 的简单 Python 脚本,它通过子进程函数调用 shell:
import subprocess
subprocess.call(['sh', './launchNastran.sh'])
我的 launchNastran.sh 是:
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN='/users/develop/tmp/input'
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
文件 TRAM 和 MODELE 与 shell 和 Python 脚本位于同一目录中。该目录可见于 Shell: CHEMIN='/users/develop/tmp/input
但是,Python 脚本中的目录发生了变化,所以我想将 Python 脚本中定义的 arg_directory 作为参数传递给 shell ,类似:
import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh'])
对于 python 脚本和像这样的 shell:
$ scriptNastran.py arg_directory
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN= arg_directory
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
有人知道怎么做吗?
感谢您的帮助:)
您可以将目录作为参数传递:
import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh', arg_directory])
然后在您的 shell 脚本中阅读它
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN=""
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch "$CHEMIN"
launchNastran.py
def call_shell(CHEMIN_path):
import subprocess
ssh_command_string='''
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN={path}
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
'''.format(path=CHEMIN_path). ## put the path you have in format
subprocess.Popen(ssh_command_string, shell=True)
call_shell('/users/develop/tmp/input') ## call the shell script
我有一个名为 'scriptNastran.py' 的简单 Python 脚本,它通过子进程函数调用 shell:
import subprocess
subprocess.call(['sh', './launchNastran.sh'])
我的 launchNastran.sh 是:
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN='/users/develop/tmp/input'
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
文件 TRAM 和 MODELE 与 shell 和 Python 脚本位于同一目录中。该目录可见于 Shell: CHEMIN='/users/develop/tmp/input
但是,Python 脚本中的目录发生了变化,所以我想将 Python 脚本中定义的 arg_directory 作为参数传递给 shell ,类似:
import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh'])
对于 python 脚本和像这样的 shell:
$ scriptNastran.py arg_directory
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN= arg_directory
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
有人知道怎么做吗?
感谢您的帮助:)
您可以将目录作为参数传递:
import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh', arg_directory])
然后在您的 shell 脚本中阅读它
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN=""
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch "$CHEMIN"
launchNastran.py
def call_shell(CHEMIN_path):
import subprocess
ssh_command_string='''
/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no
CHEMIN={path}
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch $CHEMIN
'''.format(path=CHEMIN_path). ## put the path you have in format
subprocess.Popen(ssh_command_string, shell=True)
call_shell('/users/develop/tmp/input') ## call the shell script