0

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 :)

2
  • Your goal is run the launchNastran.sh within the python OR get the result from python then pass to launchNastran.sh shell script ? Commented Jun 3, 2021 at 9:54
  • My goal is to run the launchNastran.sh within the python Commented Jun 3, 2021 at 9:55

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Simple and elegant! Thank you so much
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.