Reputation: 79
I have a shell script that has a command to run a python script. I want 4 variables (for eg: var1,var2,var3,var4) from the shell script to be used in the python script. Any suggestions how to do this?
For eg: I want to replace "lastTest44"
, firstTest44
and A-S00000582
with variables from the shell script.
driver.find_element_by_id("findKey_input").clear()
driver.find_element_by_id("findKey_input").send_keys("lastTest44")
driver.find_element_by_id("ST_View_lastTest44, firstTest44").click()
driver.find_element_by_link_text("A-S00000582").click()
Upvotes: 0
Views: 24454
Reputation: 79
I want to add for what worked for my case:
I have my variables in file which was being sourced in shell script. Need to pass the variables to python from same file. I have pandas and spark as well in my case My expected result is to concatenate the path to pass in "tocsv" which is achieved
**Shell**:
. path/to/variable/source file # sourcing the variables
python path/to/file/extract.py "$OUTBOUND_PATH"
**Python**:
import sys
import pandas as pd
#If spark session is involved import the sparksession as well
outbound_path=sys.argv[1] #argument is collected from the file passed through shell
file_name="/report.csv"
geo.topandas().tocsv(outbound_path+file_name,mode="w+")
Upvotes: 0
Reputation: 19
I think this will do what you want:
2014-06-05 09:37:57 [tmp]$ export VAR1="a"
2014-06-05 09:38:01 [tmp]$ export VAR2="b"
2014-06-05 09:38:05 [tmp]$ export VAR3="c"
2014-06-05 09:38:08 [tmp]$ export VAR4="d"
2014-06-05 09:38:12 [tmp]$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import environ
>>> environ['VAR1']
'a'
>>> environ['VAR2']
'b'
>>> environ['VAR3']
'c'
>>> environ['VAR4']
'd'
>>> environ['VAR5']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'VAR5'
Remember to catch KeyError and respond accordingly or use the get method (from the dict class) and specify a default to be used when the key is not present:
>>> environ.get('VAR5', 'not present')
'not present'
more: https://docs.python.org/2/library/os.html
Upvotes: 0
Reputation: 2561
What you're looking to use are called command line arguments. These are parameters that are specified at the time of calling the particular piece of code you're looking to run.
In Python, these are accessible through the sys
module under a variable called argv
. This is an array of all the arguments passed in from the caller, where each value within the array is a string.
For example, say the code I'm writing takes in parameters to draw a square. This could require 4 parameters - An x coordinate, y coordinate, a width, and a height. The Python code for this might look like this:
import sys
x = sys.argv[1]
y = sys.argv[2]
width = sys.argv[3]
height = sys.argv[4]
# Some more code follows.
A few things to note:
string
. This means that in this case, I could not perform any sort of arithmetic until converting them into the correct types that I want.sys.argv
is the name of the script being run. You'll want to make sure that you start reading from the second position in the array sys.argv[1]
instead of the typical zero-th index like you normally would.There is some more detailed information here, which could lead you to better ways of handling command line arguments. To get started though, this would work well enough.
Upvotes: 1
Reputation: 15501
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
Upvotes: 9