简体   繁体   English

如何用python获取shell中执行的命令&arguments

[英]How to get the command & arguments that was executed in shell with python

I am running a command in aws-cli to get the logs and i need to perform some opertaion in python with the output of this command.我正在 aws-cli 中运行一个命令来获取日志,我需要使用此命令的 output 在 python 中执行一些操作。

I am doing something like this:我正在做这样的事情:


aws logs filter-log-events --log-group-name "something" --log-stream-names my-log-stream --filter-pattern "status=FAIL" | python3 -c "import sys, json; print(json.load(sys.stdin))" 

The above command gets the aws logs and runs the python code snippet to print the json returned.上面的命令获取 aws 日志并运行 python 代码片段以打印返回的 json。

I want to access the log-group-name and log-stream-name ie "something" & "my-log-stream" (in this case) in my python code snippet.我想在我的 python 代码片段中访问log-group-namelog-stream-name ,即“某物”和“我的日志流”(在本例中)。 I am not sure how this can be done.我不确定如何做到这一点。

I have tried using sys.argv but it returns me the arguments passed in the python command ie ['-c'] and not the aws command.我试过使用sys.argv ,但它返回了 python 命令中传递的 arguments,即['-c']而不是 aws 命令。

That is not how pipe works.这不是 pipe 的工作原理。 It just sends the output of the command to the next command.它只是将命令的 output 发送到下一个命令。 You could set the log-group-name and log-stream-name as environment variables您可以将log-group-namelog-stream-name设置为环境变量

export LGN=something
...

and then access it in python with os.environ["LGN"] ... similar to this然后使用os.environ["LGN"]在 python 中访问它...类似于此

export LGN="my-log-group"; <your code here> | python3 -c "import os; print(os.environ['LGN'])"

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

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