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

Posted

技术标签:

【中文标题】如何在 python 脚本文件中使用 shell 脚本中的变量?【英文标题】:How to use variables from shell script in python script file? 【发布时间】:2014-06-05 01:22:54 【问题描述】:

我有一个 shell 脚本,它有一个运行 python 脚本的命令。我希望在 python 脚本中使用来自 shell 脚本的 4 个变量(例如:var1、var2、var3、var4)。有什么建议吗?

例如:我想用 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() 

【问题讨论】:

例如:我想用来自 shell 脚本 driver.find_element_by_id("findKey_input").clear() driver.find_element_by_id("findKey_input") 的变量替换 "lastTest44"、firstTest44 和 A-S00000582。 send_keys("lastTest44") driver.find_element_by_id("ST_View_lastTest44, firstTest44").click() driver.find_element_by_link_text("A-S00000582").click() 您需要分享到目前为止您尝试过的内容,或者您​​拥有的当前代码。尽量将其保留在相关部分,而不是整个脚本。 将变量作为命令行参数传递给python,然后使用getopt之类的东西来解析python脚本中的命令行参数。 【参考方案1】:

只需使用命令行参数:

Shell 脚本

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

Python 脚本

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

【讨论】:

非常感谢。以下是我在 python 脚本中的命令(使用 webdriver 进行测试) 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() 1. 如何在以下命令(driver.find_element_by_id("findKey_input" ).send_keys("lastTest44"))【参考方案2】:

您希望使用的称为命令行参数。这些是在调用您要运行的特定代码时指定的参数。

在 Python 中,这些可以通过名为 argv 的变量下的 sys 模块访问。这是从调用者传入的所有参数的数组,其中数组中的每个值都是一个字符串。

例如,假设我正在编写的代码接受参数来绘制一个正方形。这可能需要 4 个参数 - x 坐标、y 坐标、宽度和高度。用于此的 Python 代码可能如下所示:

import sys
x = sys.argv[1]
y = sys.argv[2]
width = sys.argv[3]
height = sys.argv[4]

# Some more code follows.

需要注意的几点:

    每个参数的类型都是string。这意味着在这种情况下,在将它们转换为我想要的正确类型之前,我无法执行任何类型的算术运算。 sys.argv 中的第一个参数是正在运行的脚本的名称。您需要确保从数组 sys.argv[1] 中的第二个位置开始读取,而不是像往常那样从典型的第零索引开始读取。

There is some more detailed information here,它可以引导您更好地处理命令行参数。不过,要开始使用,这已经足够了。

【讨论】:

【参考方案3】:

我认为这会做你想要的:

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'

记得捕获 KeyError 并做出相应的响应,或者使用 get 方法(来自 dict 类)并指定当键不存在时使用的默认值:

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

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

【讨论】:

在兴趣分享中,您不必使用命令行参数。 OP 不需要命令行参数,但这并不排除直接使用环境变量以及命令行参数(如果您愿意)。谁做了-1,有原因吗?我很想知道您为什么认为这是一个错误的答案。【参考方案4】:

我想添加对我的案例有用的内容:

我在文件中有我的变量,这些变量来自 shell 脚本。 需要将变量从同一文件传递给 python。 在我的情况下,我也有熊猫和火花 我的预期结果是连接路径以传入“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+")



【讨论】:

以上是关于如何在 python 脚本文件中使用 shell 脚本中的变量?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 linux shell 脚本中使用正则表达式搜索文件 [关闭]

python如何重新执行脚本?

调用了shell脚本的Qt程序如何打包成.deb文件

如何在 pycharm 中调试包含可执行命令的 shell 脚本文件?

如何在 Python 中使用 argparse 传递 Shell 脚本样式参数

如何使用python执行远程shell脚本