简体   繁体   English

在Python中执行复杂的bash脚本

[英]Execute complex bash script within Python

I have a bash script which I run on my .csv file and then I run python script on the output of bash script. 我有一个bash脚本,该脚本在.csv文件上运行,然后在bash脚本的输出上运行python脚本。 I would like to make everything into a single script, but bash scrip is quite complex and I couldn't find a way to use it in a Python.. 我想将所有内容制作成一个脚本,但是bash脚本非常复杂,我找不到在Python中使用它的方法。

grep "$(grep -E "tcp|udp" results.csv | grep -E "Critical|High|Medium" | awk -F "\"*,\"*" '{print $8}')" results.csv | sort -t',' -k4,4 -k8,8 | awk -F "\"*,\"*" '{print $5,"port",$7"/"$6,$8}' | sed '/tcp\|udp/!d' | awk '!a[$0]++' | sed '/,port,\/,/d' > out

I tried this both as a string, and as a parametrized command with subprocess, however it's just seems way too many complex characters for everything to work. 我既尝试将其作为字符串,又作为带有子进程的参数化命令进行了尝试,但是似乎复杂的字符太多了,一切都无法正常工作。

Is there a way simpler way to run this command in Python? 有没有一种更简单的方法可以在Python中运行此命令?

PS I know there are multiple questions & answers regarding this same topic, but none of them worked for me. PS:我知道关于同一主题有多个问题和答案,但是没有一个对我有用。

Could you please escape all the " double quotes" with \\ please try it out and let us know if it worked: 您可以使用\\逸出所有的“双引号”吗,请尝试一下并让我们知道它是否有效:

os.system(" grep \"$(grep -E \"tcp|udp\" results.csv | grep -E \"Critical|High|Medium\" | awk -F \"\\\"*,\\\"*\" '{print $8}')\" results.csv | sort -t',' -k4,4 -k8,8 | awk -F \"\\\"*,\\\"*\" '{print $5,\"port\",$7\"/\"$6,$8}' | sed '/tcp\|udp/!d' | awk '!a[$0]++' | sed '/,port,\/,/d' > out ")

The whole command can be put into " your_command_with\\"escaped\\"double quotes ". 整个命令可以放在“ your_command_with \\”转义\\“双引号中。 Have a nice day 祝你今天愉快

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

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