当前位置:网站首页>Pipes and xargs

Pipes and xargs

2022-04-23 20:59:00 Sleeping snail

ps aux|grep deltest|grep -v grep|awk '{print $2}'|xargs kill -9

ps aux Will list all processes , The next pipe will pass the result to the second command

The second command looks for... From the result of the first command deltest

The third command is to filter the text backwards , Will include grep The deletion of

The fourth command will read the second column of data of the result deleted by the third command ,awk By default, they are separated by spaces

xargs A command is a filter used to pass parameters to other commands , It's also a tool for combining multiple commands .

Successful conversion of standard input data command line parameters can convert the data in the pipeline or standard input into command line parameters of specific commands

The fifth command will treat the previous command as kill And then kill

 

xargs

If you want to run a file in the current directory , have access to python3 `pwd`/deltest2.py

You can also use pwd | xargs -I {} python3 {}/deltest2.py

This -I{} It should be to put the content from the front into {}, Then use it again {} For the use of

and echo `pwd|xargs -I {} python3  {}/$element` It should be that you want to output the return value of the run , If the program keeps running , Then it will always wait . 

Let multiple programs run sh command

#!/bin/bash
array=("deltest2.py" "deltest.py")

cd /workplace/kill_test
nowpath='/workplace/kill_test/'
for element in ${array[@]}
    do
    echo $element
   nohup pwd|xargs -I {} python3  {}/$element &
   sstr=$(echo -e $str)
   echo $sstr
    done

Batch stop program

#!/bin/bash
# Try to write a fully running program , You'd better add the suffix    There are no commas between array elements 
array=("deltest2.py" "deltest.py")


for element in ${array[@]}
    do
    ps aux|grep $element|grep -v grep|awk '{print $2}'|xargs kill -9
    done

版权声明
本文为[Sleeping snail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232049234446.html