-1
我有一个配置文件,用户可以在其中指定一组shell命令。 命令基本上是一系列由管道分隔的shell命令。执行从Python中的文件读取为字符串的复杂Shell命令
CMD1 = grep "SomeOtherString" | grep "XX" | cut -d":" -f9 | cut -d"," -f1
CMD2 = grep "SomeOtherString" | tail -1| cut -d":" -f9 | cut -d"," -f1 | cut -d"[" -f2 | cut -d"]" -f1
我能够读取我的Python脚本中的命令。我的问题是,我将如何能够在Python中运行这些读取命令字符串并获得输出。
任何解决方案与subprocess
,plumbum
,sh
将是可以接受的。
如果您了解子流程,为什么不尝试呢? https://docs.python.org/3/library/subprocess.html,https://stackoverflow.com/documentation/python/1393/subprocess-library#t=201704070552204503464 – Sundeep
另外,您的管道看起来像是他们想要的改为小Awk脚本。 'CMD1 = awk -F:'/ SomeOtherString/&&/XX/{s = $ 9; sub(/,.*/,“”,s); print s}''和'CMD2 = awk -F:'/ SomeOtherString {s = $ 9; sub(/)[^],]*,.*/,“”,s); sub(/^[^ [] * \ [/,“”,s); } END {print s}'' – tripleee
@tripleee是的,它也可以有awk命令:grep“Something”$ {LOGFILE} | tail -1 | awk -F“=”'{print $ 3}' – VikramChopde