elle.delle
elle.delle

Reputation: 328

Shell script taking for argument a Python variable

I have a simple Python script named 'scriptNastran.py' that calls a shell through the subprocess function:

import subprocess

subprocess.call(['sh', './launchNastran.sh'])

My launchNastran.sh is :

/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

The files TRAM and MODELE are in the same directory as the shell and the Python script. That directory is seen in the Shell: CHEMIN='/users/develop/tmp/input

However, the directory changes in the Python script, so I would like to pass the arg_directory defined in the Python script as an argument for the shell, something like:

import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh'])

For the python script and like this for the 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

Does someone knows how to do it?

Thank you for your help :)

Upvotes: 0

Views: 68

Answers (2)

Bao Tran
Bao Tran

Reputation: 626

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

Upvotes: 1

Kerrim
Kerrim

Reputation: 523

You can pass the directory as an argument:

import subprocess
arg_directory = 'hello world'
subprocess.call(['sh', './launchNastran.sh', arg_directory])

and then read it in your shell script

/appli/nastran -msc20121 TRAM mem=512M buffsize=25601 batch=no    
CHEMIN="$1"
\cp -rf TRAM MODELE ./*f06 ./*f04 ./*log ./*op2 ./*pch "$CHEMIN"

Upvotes: 2

Related Questions