Linux 中的 &> /dev/null

其实,简单理解的话:command > /dev/null 2>&1 和 command &> /dev/null 是一样的。

专业点解释就是:将标准错误重定向到标准输出,然后将标准输出(标准错误和标准输出)重定向到 黑洞(/dev/null)!

简单点理解就是:把命令的标准输出和标准错误全都扔了!

command > /dev/null 2>&1

[root@master ~]# ll no_exist.txt
ls: cannot access no_exist.txt: No such file or directory
[root@master ~]# ll no_exist.txt > /dev/null
ls: cannot access no_exist.txt: No such file or directory
[root@master ~]# ll no_exist.txt > /dev/null 2>&1

command &> /dev/null

[root@master ~]# ll no_exist.txt
ls: cannot access no_exist.txt: No such file or directory
[root@master ~]# ll no_exist.txt > /dev/null
ls: cannot access no_exist.txt: No such file or directory
[root@master ~]# ll no_exist.txt &> /dev/null

当然,如果你想把标准输出和标准错误保存下来的话,/dev/null 也可以替换成具体的输出文件。

在 python3 的 subprocess 里边对应的语法:

先贴个官网的链接:subprocess --- 子进程管理 — Python 3.7.12 文档

你可以把下面的 ”=“ 理解为重定向。

import subprocess

subprocess.run(['command', 'param1', 'param2', 'param...'],
               stderr=subprocess.STDOUT, stdout=subprocess.DEVNULL)

像 subprocess.Popen 也是类似的:

popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)

 

这样的话,你就不用再写 os.system() (好多项目会觉得这个命令比较危险,写上的话基本上会被毙掉的!)了:

import os

os.system('command param1 param2 param... &> /dev/null')


# import subprocess
# subprocess.getstatusoutput('command param1 param2 param... &> /dev/null')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值