我想知道什么时候应该使用管道,什么时候不应该使用。
例如,要杀死某些处理 pdf 文件的进程,使用管道将无法执行以下操作:
ps aux | grep pdf | awk '{print $2}'|kill
Run Code Online (Sandbox Code Playgroud)
相反,我们只能通过以下方式做到这一点:
kill $(ps aux| grep pdf| awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)
或者
ps aux | grep pdf | awk '{print $2}'| xargs kill
Run Code Online (Sandbox Code Playgroud)
根据man bash
(版本4.1.2
):
The standard output of command is connected via a pipe to the standard input of command2.
Run Code Online (Sandbox Code Playgroud)
对于上述场景:
grep
是标准输出ps
。那个有效。awk
是标准输出grep
。那个有效。kill
是标准输出awk
。那行不通。以下命令的标准输入总是从前一个命令的标准输出获得输入。
kill
or 一起使用rm
?我发现如果我们使用awk 0 inputfile
,它不会打印任何内容,原因0
是条件错误。
如果我们使用awk 1 inputfile
,它会将所有内容打印为1
awk 解释的每一行都为真。
如果我们使用awk any_string inputfile
,它不会打印任何内容,因为所有 awk 变量都初始化为零,因此为 false。
但是如果我们使用awk any_integer inputfile
,它会变成真的并打印文件的每一行,我可以知道是什么原因吗?
不过,我在GNUawk
手册中找不到对此的解释。
我正在研究如何让iproute2命令替换旧的ifconfig
和ifup
ifdown
命令,我发现了一些有趣的东西。
我的网卡设置是:
[16:07:41 root@vm network-scripts ]# cat /etc/sysconfig/network-scripts/ifcfg-eth2
DEVICE=eth2
ONBOOT=no
BOOTPROTO=dhcp
Run Code Online (Sandbox Code Playgroud)
要打开和关闭界面,旧的方法是:
ifup eth2
ifdown eth2
[16:25:10 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff
[16:25:14 root@vm network-scripts ]# ifup eth2
Determining IP information for eth2... done.
[16:25:22 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:b8:13:b4 brd …
Run Code Online (Sandbox Code Playgroud)