1

我正在尝试通过以下方式将几个参数从 python 脚本传递到 bash 脚本:

input_string = pathToScript + " " + input1 + " " + input2 + " " + input3 + " " + input4 + " " + \
input5 + " " + input6 + " "  + input7 + " " + input8 + " "  + input9 + " "  + input10

args =  shlex.split(input_string)

p = subprocess.Popen(args)

所有的inputX都是字符串。这些参数中的每一个都应传递给脚本,其路径存储在变量中pathToScript。我现在希望能够像往常一样在 bash 脚本中捕获这些参数:

#No input check yet...
history_file = "$1"
folder_history_file = "$2"
folder_bml_files = "$3"
separate_temperature = "$4"
separate_temperature_col_index = "$5"
separate_sight = "$6"
separate_sight_col_index = "$7"
separate_CO = "$8"
separate_CO_col_index = "$9"
separate_radiation = "$10"

这会导致line 61: separate_CO_col_index: command not found所有这些行的错误,并且错误不会以与行排序相同的方式出现。换句话说,第 61 行的此类错误有时会在第 60 行的错误之前被捕获,这似乎来自 Eclipse 中的输出(我在 Eclipse 中使用 PyDev)。

不能像这样运行bash脚本吗?我已经尝试按照 ch 中的方法进行操作。17.1.1.1 这里,但我可能没有正确理解:python docs

4

1 回答 1

4

bash 中的错误出现是因为您在=. 你应该使用

history_file="$1"

代替

history_file = "$1"

当您在此处放置空格时,Bash 认为该行是命令调用并尝试history_file作为命令运行。


在您的 Python 脚本中,您可以简单地使用:

args = [pathToScript, input1, input2, ....]

而不是你所拥有的。这更简单,并且如果参数包含空格(您当前的代码将失败),它将正常工作。构建input_string字符串只是为了将其拆分回下一行是没有意义的。

于 2013-02-04T15:21:02.467 回答