Chinaunix

标题: shell脚本中的“grep”疑问 [打印本页]

作者: chenduo911    时间: 2010-01-22 11:41
标题: shell脚本中的“grep”疑问
各位前辈们,新人请教个问题,请看脚本中的语句

if grep "bubu" /root/test.txt > /dev/null 2>&1
then
echo "bubu is in test.txt!"
else
echo "bubu is not in test.txt!"

该语句大家都能看出来是用户判断字符串“bubu”是否在test.txt中,我的疑问是/dev/null 2>&1是什么意思? 其中&1我猜测应该是为真(即if语句的判断部分为真),另外为何要用到重定向符以及该路径、还有数字2是什么意思?谢谢大家的解答!
作者: juffun    时间: 2010-01-22 11:43
标题: 回复 #1 chenduo911 的帖子
> /dev/null   是将输出指向/dev/null
2>&1            是将标准错误输出指向标准输出,即如果出错,则错误消息也写到/dev/null这里
作者: bbgg1983    时间: 2010-01-22 11:43
1是标准输出,2是标准错误输出,> /dev/null是把标准输出定向到/dev/null设备,2>&1是把标准错误输出也仍到这个地方去
作者: Shell_HAT    时间: 2010-01-22 13:26
十三问:
http://bbs.chinaunix.net/viewthr ... p;page=7#pid1636825
作者: foxyzhou    时间: 2010-01-22 16:40
楼上发的传送门是个好东西




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2