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.
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
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 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))
Pass variable from Python to Bash
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"
Suppose for some reason Python is needed to spit out a bunch of
bash
variables and assignments, or (cautiously) compose code on-the-fly. Aneval
method:myvar=first
eval "$(python - <<< "print('myvar=second')" )"
echo "$myvar"
Related Topics
Change Parent Shell's Environment from a Subprocess
How to Add File Extensions Based on File Type on Linux/Unix
How to Generate Dynamic (Parameterized) Unit Tests in Python
Creating a JSON Response Using Django and Python
How to Hide the Console When I Use Os.System() or Subprocess.Call()
How to See the Entire Http Request That's Being Sent by My Python Application
Why Do I Get "Typeerror: 'Str' Object Is Not Callable" with This Code
How to Get the Caller's Method Name in the Called Method
How to One Hot Encode in Python
Python Library for Linux Process Management
Calling Parent Class _Init_ with Multiple Inheritance, What's the Right Way
How to Create a Zip Archive of a Directory
Copying Nested Lists in Python
Group by in Group by and Average