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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24050168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:37:22  来源:igfitidea点击:

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

pythonbash

提问by texassiga

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?

我有一个 shell 脚本,它有一个运行 python 脚本的命令。我想在 python 脚本中使用来自 shell 脚本的 4 个变量(例如:var1,var2,var3,var4)。任何建议如何做到这一点?

For eg: I want to replace "lastTest44", firstTest44and A-S00000582with 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() 

回答by ooga

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

回答by pseudoramble

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 sysmodule 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.

在 Python 中,这些可以通过sys名为argv. 这是一个包含从调用者传入的所有参数的数组,其中数组中的每个值都是一个字符串。

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:

例如,假设我正在编写的代码接受参数来绘制一个正方形。这可能需要 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.

A few things to note:

需要注意的几点:

  1. Each argument is of type 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.argvis 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.
  1. 每个参数都是 类型string。这意味着在这种情况下,在将它们转换为我想要的正确类型之前,我无法执行任何类型的算术运算。
  2. 第一个参数sys.argv是正在运行的脚本的名称。您需要确保从数组中的第二个位置开始读取,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.

这里有一些更详细的信息,可以引导您更好地处理命令行参数。不过,要开始使用,这足够了。

回答by hsum

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