简体   繁体   English

如何在python脚本文件中使用shell脚本中的变量?

[英]How to use variables from shell script in python script file?

I have a shell script that has a command to run a python script.我有一个 shell 脚本,它有一个运行 python 脚本的命令。 I want 4 variables (for eg: var1,var2,var3,var4) from the shell script to be used in the python script.我想在 python 脚本中使用来自 shell 脚本的 4 个变量(例如:var1,var2,var3,var4)。 Any suggestions how to do this?任何建议如何做到这一点?

For eg: I want to replace "lastTest44" , firstTest44 and A-S00000582 with variables from the shell script.例如:我想用 shell 脚本中的变量替换"lastTest44"firstTest44A-S00000582

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

Just use command line arguments:只需使用命令行参数:

Shell Script外壳脚本

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

Python Script Python脚本

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

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 .在 Python 中,这些可以通过名为argv的变量下的sys模块访问。 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.这可能需要 4 个参数 - x 坐标、y 坐标、宽度和高度。 The Python code for this might look like this:用于此的 Python 代码可能如下所示:

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:需要注意的几点:

  1. Each argument is of type string .每个参数都是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.这意味着在这种情况下,在将它们转换为我想要的正确类型之前,我无法执行任何算术运算。
  2. The first argument in sys.argv is the name of the script being run. sys.argv的第一个参数是正在运行的脚本的名称。 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.您需要确保从数组sys.argv[1]的第二个位置开始读取,而不是像通常那样从典型的第零个索引开始读取。

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.不过,要开始使用,这已经足够了。

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:请记住捕获 KeyError 并做出相应的响应,或者使用 get 方法(来自 dict 类)并指定当键不存在时使用的默认值:

>>> environ.get('VAR5', 'not present')
'not present'

more: https://docs.python.org/2/library/os.html更多: https : //docs.python.org/2/library/os.html

I want to add for what worked for my case:我想添加对我的案例有用的内容:

I have my variables in file which was being sourced in shell script.我的文件中有我的变量,该文件来源于 shell 脚本。 Need to pass the variables to python from same file.需要将变量从同一个文件传递给 python。 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在我的情况下,我也有 pandas 和 spark 我的预期结果是连接传递“tocsv”的路径,这是实现的

**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+")



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM