How to Pass Variables from Python Script to Bash Script

How to pass variables from python script to bash script

I would print it to a file chosen on the command line then I'd get that value in bash with something like cat.

So you'd go:

python b.py tempfile.txt
var=`cat tempfile.txt`
rm tempfile.txt

[EDIT, another idea based on other answers]

Your other option is to format your output carefully so you can use bash functions like head/tail to pipe only the first/last lines into your next program.

Passing variable to bash script from Python

Firstly, in the bash file you are supposed to do:

#!/bin/bash
clustername=$1 # invoking from pythonscript

to get the command line arguments passed into the script.

Secondly, using check_call(), you will not be able to catch the output from the bash script. You will need to use subprocess.run() or subprocess.check_output() or something similar to achieve that.

You can try doing the following:

import sys
import subprocess

clustername=sys.argv[1]
print(subprocess.run(["/data/path/script.sh", str(clustername)], universal_newlines=True).stdout)

check_call() will only return the return code and not the stdout. You can also try check_output() if you want to see the output.

You can also do the following using check_output:


print(subprocess.check_output(["/data/path/script.sh", str(clustername)], universal_newlines=True))
Edit

Removed shell=True as suggested in the comments.

Pass variable from Python to Bash

  1. Unless Python is used to do some kind of operation on the original data, there's no need to import anything. The answer could be as lame as:

     myvar=$(python - <<< "print 'second'") ; echo "$myvar"
  2. Suppose for some reason Python is needed to spit out a bunch of bash variables and assignments, or (cautiously) compose code on-the-fly. An eval method:

     myvar=first
    eval "$(python - <<< "print('myvar=second')" )"
    echo "$myvar"

Pass a variable from python to shell script

If call(['bash', 'run.sh']) is working without arguments, there is no reason why it shouldn't work when additional arguments are passed.

You need to substitute the values of the variables into the command line arguments, not just pass the names of the variables as strings as does this:

call(['bash', 'run.sh', 'var1', 'var2'])

Instead, do this:

var1 = '1'
var2 = '2'
call(['bash', 'run.sh', var1, var2])

Now this will work providing that var1 and var2 are strings. If not, you need to convert them to strings:

var1 = 1
var2 = 2
call(['bash', 'run.sh', str(var1), str(var2)])

Or you can use shlex.split():

cmd = 'bash run.sh {} {}'.format(var1, var2)
call(shlex.split(cmd))

Call Python script from bash with argument

To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance

> python python_script.py var1 var2

To access these variables within python you will need

import sys
print(sys.argv[0]) # prints python_script.py
print(sys.argv[1]) # prints var1
print(sys.argv[2]) # prints var2

How to use variables from shell script in python script file?

Just use command line arguments:

Shell Script

a=1
b=2
python test1.py "$a" "$b"

Python Script

import sys
var1 = sys.argv[1]
var2 = sys.argv[2]
print var1, var2


Related Topics



Leave a reply



Submit