Linux系列-Linux多命令协作:管道及重定向

1:linux中的输入输出

名称      说明      编号      默认 

stdin 标准输入      0       键盘 
stdout 标准输出   1       终端 
stderr 标准错误    2       终端

2:基本输入输出

通过管道和重定向我们可以控制CLI的数据流

< 将标准输入(不包含错误信息)重定向到文件 (以覆盖的形式) 

echo "hello world" < aa.file 

<< 将标准输入重定向到文件(以追加的形式) 

echo "hello world" << aa.file 

2< 将标准错误重定向到文件 

2<&1 将标准输入和标准错误结合在一起 

< stdin 

grep java > /etc/passwd 把passwd文件作为输入命令。 

3:管道  将一个命令的标准输出作为另外一个命令的标准输入 

ls -l | grep java  从 列出来的目录中查找关键字 


网上找了一些管道使用的列子 大家可以看下

[chengmo@centos5 shell]$ cat test.sh | grep -n 'echo'
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#读出test.sh文件内容,通过管道转发给grep 作为输入内容
 
[chengmo@centos5 shell]$ cat test.sh test1.sh | grep -n 'echo'
cat: test1.sh: 没有那个文件或目录
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#cat test1.sh不存在,错误输出打印到屏幕,正确输出通过管道发送给grep
 
 
[chengmo@centos5 shell]$ cat test.sh test1.sh 2>/dev/null | grep -n 'echo' 
5:    echo "very good!";
7:    echo "good!";
9:    echo "pass!";
11:    echo "no pass!";
#将test1.sh 没有找到错误输出重定向输出给/dev/null 文件,正确输出通过管道发送给grep
 
 
[chengmo@centos5 shell]$ cat test.sh | ls
catfile      httprequest.txt  secure  test            testfdread.sh  testpipe.sh    testsh.sh      testwhile2.sh
envcron.txt  python           sh      testcase.sh     testfor2.sh    testselect.sh  test.txt       text.txt
env.txt      release          sms     testcronenv.sh  testfor.sh     test.sh        testwhile1.sh
#读取test.sh内容,通过管道发送给ls命令,由于ls 不支持标准输入,因此数据被丢弃

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值