简体   繁体   English

如何在 Python 中编写相同的 shell 脚本?

[英]How do I write the same shell script in Python?

I am new to Python.我是 Python 的新手。 I am attempting to convert my shell script to Python language.我正在尝试将我的 shell 脚本转换为 Python 语言。 I have a piece of code as below in shell, to be converted to Python.我在shell中有一段代码如下,要转换为Python。

Shell code I wrote:我写的 Shell 代码:

f_consolidate_output_test()
{
 v_index=$2
 eval "v_$1_list[${v_index}]"=${v_line}
 #echo "Pass list[${v_index}] : ${v_pass_list[${v_index}]}";
 #echo "Fail list[${v_index}] : ${v_fail_list[${v_index}]}";
}

This function has two arguments argument1 is a string it has values either pass or fail and second argument is index number.这个 function 有两个 arguments 参数 1 是一个字符串,它的值是通过或失败,第二个参数是索引号。

If I receive first argument value as "pass", I build an array as "v_pass_list", if I receive value as "fail" I build an array with name v_fail_list using the index received as second argument.如果我收到第一个参数值作为“通过”,我构建一个数组作为“v_pass_list”,如果我收到值作为“失败”,我使用作为第二个参数接收的索引构建一个名称为 v_fail_list 的数组。

This same thing I need in Python.我在 Python 中也需要同样的东西。

Please help me on this.请帮助我。

Thank you so much in advance.非常感谢你。

Regards, Macharla Ramesh Kumar问候, 马查拉·拉梅什·库马尔

For "variable variables" ( v_$1_list ), you would use a dict that maps the $1 to something else.对于“变量变量”( v_$1_list ),您将使用将$1映射到其他内容的字典。

v_lists = {"pass": {}, "fail": {}}

def f_consolidate_output_test(flag, v_index, v_line):
    v_lists[flag][v_index] = v_line

This will happily crash if flag is not "pass" or "fail" .如果flag不是"pass""fail" ,这将很高兴地崩溃。

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

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