根据存在的命令杀死Linux进程(Kill Linux processes based on commands present)
我正在学习Linux并且需要编写一个shell脚本,只要在其上运行另一个命令就会终止所有进程。 我知道如何使用变量编写脚本,但似乎无法找到为命令执行此操作的方法。
我想我需要找到一种方法来评估命令是否为真,并将其用作if语句的条件。 但我尝试的任何东西都会返回错
Linux是我学习的第一个CS相关的东西,我绝对坚持这一步。 我试着寻找这个,但我不确定要写什么。
这是sh shell。
编辑:每当使用某个命令执行程序时,我需要脚本立即终止它。
I am learning Linux and need to write a shell script that will kill all processes whenever another command runs on it. I know how to write a script with a variable, but cannot seem to find ways of doing it for a command.
I would imagine that I need to find a way to evaluate if the command is true or not and to use that as the condition for an if statement. But anything I try returns errors.
Linux is the first CS related thing that I am learning and I am absolutely stuck at this step. I tried searching for this but am not quiet sure what to write.
It's the sh shell.
Edit: Whenever a program is executed with a certain command I need the script to terminate it right away.
原文:https://stackoverflow.com/questions/32935347
最满意答案
你可以使用
grep \bword\b
,这是在bash解析反斜杠之后真的是grep bwordb
。使用
grep '\bword\b'
(注意单引号)。You are proabably using
grep \bword\b
which is reallygrep bwordb
after bash parses the backslashes.Use
grep '\bword\b'
(note the single-quotes).
相关问答
更多-
我想编写一个正则表达式,它匹配{A:"和之前",RC:之后的日期和字符串",RC: 你可以使用这个grep: grep -oP '(?<=A:").*?(?=",RC:)' file 0b87654nuy I wanted to write a regex that matches the date and string that comes after {A:" and before ",RC: You can use this grep: grep -oP '(?<=A:").*?(?=",RC:)' ...
-
{n}是重复计数,它恰好匹配左侧原子的n副本。 所以我们可以将这个表达式细分为以下部分: [0-9] {8} - match 8 digits 030 - match "030" exactly [0-9] {4} - match 4 digits 因此,此模式匹配包含"########030####"任何内容,其中每个#是0到9的数字。 如果它检查的字符串的形式是YYYYmmddHHMMSS ,似乎就是这种情况,这个模式似乎是为了匹配凌晨3点到凌晨3点10分之间 ...
-
您可以在regeular表达式中使用OR模式。 尝试以下配置。
@type grep regexp1 level (ERROR|FATAL) You can use OR pattern in regeular expression. Try following configuration.@type grep regexp1 level (ERROR|FATAL) -
如何grep特定模式(How to grep a particular pattern)[2021-09-07]
如果您的目标只是为了“lubuntu”,而不是“lubuntu-somethingelse”,那么您可以在之后添加一个空格: ps -eo pid,cmd | grep "qemu.*-name lubuntu " | grep -v grep | awk '{print $1}' 或者没有额外的命令: pgrep -f "qemu.*-name lubuntu " 编辑:如果你需要一些可靠和通用的东西,而不是你自己的脚本,那么解析命令行总是一个坏主意。 有太多事情可能出错。 由于您使用libvirt来 ... -
在unix上有一个实用程序可以删除char 13,'\ r'或^ M. 语法可能有所不同 dos2unix optionsList.txt dos2unix optionsList.txt optionsList.txt There is an utility on unix to remove char 13, '\r' or ^M. Syntax may vary dos2unix optionsList.txt dos2unix optionsList.txt optionsList.txt
-
使用\ b和grep模式(using \b with grep pattern)[2023-07-31]
你可以使用grep \bword\b ,这是在bash解析反斜杠之后真的是grep bwordb 。 使用grep '\bword\b' (注意单引号)。 You are proabably using grep \bword\b which is really grep bwordb after bash parses the backslashes. Use grep '\bword\b' (note the single-quotes). -
你可以利用前瞻?= 。 v_neutral(?=\\s+vaux_neutral) ?=\\s+vaux_neutral :向前看是否有一个或多个空格后跟vaux_neutral v_neutral :如果满足前瞻条件,则匹配v_neutral 看看演示 you can make use of a lookahead ?=. v_neutral(?=\\s+vaux_neutral) ?=\\s+vaux_neutral: looks ahead to see if there is one or more ...
-
Grep模式重复(Grep Pattern Repetition)[2023-03-15]
假设没有任何尴尬的东西像在其中有逗号的字段(因为如果前8个字段中有这样的字段,则无法在没有完整的CSV识别工具的情况下处理文件),并且存在第9个字段(因此第7个字段)第8个字段后面都有逗号)然后: grep '^\([^,]*,\)\{6\}\([^,]*,\)\2' file.csv 第一位表示6个零或多个非逗号序列,每个逗号后跟一个逗号。 然后是第7个(可能是空的)字段及其尾随逗号; 然后又是同样的东西( \2 )。 $ cat file.csv a,b,c,d,e,f,g,g,i a,b,c,d,e ... -
AWK 这个awk one-liner会有所帮助: awk 'NR==FNR{if(/explorer[.]exe/)d[++i]=NR;next} {for(x=1;x<=i;x++)if(FNR>=d[x]-1&&FNR<=d[x]+4)next}7' file file 看这个例子: kent$ cat f foo foo2 Rule: r1 Owner: Process explorer.exe Pid 1544 remove1 remove2 remove3 remove4 b ...
-
不使用任何捕获组, > my.list<-list(a='ivw_2014_abc.pdf',b='ivw_2014_def.pdf',c='ivw_2014_ghi.pdf') > gsub("^.*_|\\..*$", "", my.list, perl=T) [1] "abc" "def" "ghi" Without using any capturing groups, > my.list<-list(a='ivw_2014_abc.pdf',b='ivw_2014_def.pdf',c='ivw ...