简体   繁体   English

如何使用 grep 捕获字段发出命令?

[英]How to make a command with grep captured fields?

I'm trying to capture a process that runs and make an strace of it in one line:我正在尝试捕获一个正在运行的进程并在一行中对其进行跟踪:

I managed to capture only the one that i want with this command我设法用这个命令只捕获了我想要的那个

ps -ef | grep "[0-9].*[0-9] /usr/bin/python3 /home/pi/readcard.py"

outputs this:输出这个:

root       676   668 99 11:00 ?        00:34:21 /usr/bin/python3 /home/pi/readcard.py

Now I'm trying to capture the process pid with this regex and use it to make another command:现在我正在尝试使用此正则表达式捕获进程 pid 并使用它来创建另一个命令:

ps -ef | grep "([0-9]+).*[0-9] /usr/bin/python3 /home/pi/readcard.py"

How I could make to run something like this?我怎么能运行这样的东西?

sudo strace -f -p{captured_field} -s9999 -e write

Use awk for only showing the second column:使用awk仅显示第二列:

ps -ef | grep "([0-9]+).*[0-9] /usr/bin/python3 /home/pi/readcard.py" | awk '{print $2}'

This, you can use it as an input for another command, by embedding it into $(...) , as follows:这样,您可以通过将其嵌入到$(...)中,将其用作另一个命令的输入,如下所示:

sudo strace -f -p{$(ps -ef | grep "([0-9]+).*[0-9] /usr/bin/python3 /home/pi/readcard.py" | awk '{print $2}')} -s9999 -e write

Good luck祝你好运

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

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