Shell学习05--Shell输入和输出重定向

5 篇文章 0 订阅


输入:
一般都是从键盘读取用户输入的数据,然后再把数据拿到程序中使用(标准输入方向)
键盘 ——>程序

输出:
反过来程序也会产生数据,这些数据直接呈现到显示器上(标准输出方向)
程序——>显示器

输入方向:数据从哪里走向程序。默认从键盘到程序,如果改变了它的方向则为输入重定向
输出方向:数据从程序走向哪里。默认从程序到键盘,如果改变了它的方向则为输出重定向

文件描述符文件名类型硬件
0stdin标准输入文件键盘
1stdout标准输出文件显示器
2stderr标准错误输出文件显示器

1.Linux的输出重定向

类型符号作用
标准输出重定向command > file以覆盖的方式,把command的正确输出结果放入file中
标准输出重定向command >> file以追加的方式,把command的正确输出结果放入file中
标准错误输出重定向command 2 > file以覆盖的方式,把 command 的错误信息输出到 file 文件中
标准错误输出重定向command 2 >> file以追加的方式,把 command 的错误信息输出到 file 文件中
正确输出和错误信息同时保存command > file 2>&1以覆盖的方式,把正确输出和错误信息同时保存到同一个文件(file)中
正确输出和错误信息同时保存command >> file 2>&1以追加的方式,把正确输出和错误信息同时保存到同一个文件(file)中
正确输出和错误信息同时保存command >file1 2>file2以覆盖的方式,把正确输出保存到(file1)当中,错误信息保存到(file2)当中
正确输出和错误信息同时保存command >>file1 2>>file2以追加的方式,把正确输出保存到(file1)当中,错误信息保存到(file2)当中

注释
>代表覆盖
>>代表追加
输出里,fd>>file(fd为文件描述符),fd默认为1可以不写
举例:
commad >> file == command 1>> file 加1多此一举

格式重点
fd>>>之间不能有空格,必须连着
command >>或者> file之间的空格可有可无

举例:
1.查看当前目录的文件

[root@admin world]# ls -l > test02
[root@admin world]# cat test02
总用量 72
-rw-r--r--. 1 root root    0 723 09:47 444
-rwxr-xr-x. 1 root root  282 85 16:02 demo01.sh
-rwxr-xr-x. 1 root root  167 85 17:08 demo02.sh
-rwxr-xr-x. 1 root root  237 85 17:23 demo03.sh
-rwxr-xr-x. 1 root root   63 85 17:54 demo04.sh
-rwxr-xr-x. 1 root root   17 85 21:44 demo05.sh
-rwxr-xr-x. 1 root root  216 86 08:59 demo06.sh
-rwxr-xr-x. 1 root root  605 86 09:21 demo07.sh
-rwxr-xr-x. 1 root root 1341 86 09:57 demo08.sh
-rwxr-xr-x. 1 root root  332 86 11:14 demo09.sh
-rwxr-xr-x. 1 root root  329 86 13:16 demo10.sh
-rwxr-xr-x. 1 root root   36 86 13:17 demo11.sh
-rwxr-xr-x. 1 root root   59 85 15:23 demo.sh
-rwxr-xr-x. 1 root root   91 86 14:08 for01.sh
-rwxr-xr-x. 1 root root  235 86 15:50 function01.sh
-rw-------. 1 root root   26 84 11:24 nohup.out
-rw-r--r--. 1 root root   40 86 15:55 test
-rw-r--r--. 1 root root   17 86 15:51 test01
-rw-r--r--. 1 root root    0 86 16:08 test02
-rw-r--r--. 1 root root    5 85 11:06 tests01

2.把正确结果和错误信息都保存到一个文件中

[root@admin world]# ls -l > test02 2>&1
[root@admin world]# ls java >> test02 2>&1
[root@admin world]# cat test02
总用量 72
-rw-r--r--. 1 root root    0 723 09:47 444
-rwxr-xr-x. 1 root root  282 85 16:02 demo01.sh
-rwxr-xr-x. 1 root root  167 85 17:08 demo02.sh
-rwxr-xr-x. 1 root root  237 85 17:23 demo03.sh
-rwxr-xr-x. 1 root root   63 85 17:54 demo04.sh
-rwxr-xr-x. 1 root root   17 85 21:44 demo05.sh
-rwxr-xr-x. 1 root root  216 86 08:59 demo06.sh
-rwxr-xr-x. 1 root root  605 86 09:21 demo07.sh
-rwxr-xr-x. 1 root root 1341 86 09:57 demo08.sh
-rwxr-xr-x. 1 root root  332 86 11:14 demo09.sh
-rwxr-xr-x. 1 root root  329 86 13:16 demo10.sh
-rwxr-xr-x. 1 root root   36 86 13:17 demo11.sh
-rwxr-xr-x. 1 root root   59 85 15:23 demo.sh
-rwxr-xr-x. 1 root root   91 86 14:08 for01.sh
-rwxr-xr-x. 1 root root  235 86 15:50 function01.sh
-rw-------. 1 root root   26 84 11:24 nohup.out
-rw-r--r--. 1 root root   40 86 15:55 test
-rw-r--r--. 1 root root   17 86 15:51 test01
-rw-r--r--. 1 root root    0 86 16:12 test02
-rw-r--r--. 1 root root    5 85 11:06 tests01
ls: 无法访问java: 没有那个文件或目录 #错误信息提示

上面的实例将正确结果和错误信息都写入同一个文件中,这样会导致视觉上的混乱,不利于以后的检索,所以我建议把正确结果和错误信息分开保存到不同的文件中,也即写成下面的形式:

ls -l >>out.log 2>>err.log

把正确列表放在out.log中
把错误列表放在err.log中
这样可以方便观察结果

/dev/null 文件
/dev/null 文件 为Linux系统的垃圾箱,任何放入的数据都会被丢弃不能恢复

2.Linux的输入重定向

输入重定向就是改变输入的方向,不再使用键盘作为命令输入的来源,而是使用文件作为命令的输入。

符号说明
command < file将file里的内容作为command的输入
command << EOF从标准输入(键盘中)读取数据,只要遇到分界符EOF位置,EOF(自己设定,随便一个字符)
command <file1 >file2将 file1的内容作为 command 的输入,并将 command 的处理结果输出到 file2。

注释:
和输出重定向类似,输入重定向的完整写法是fd<file,其中 fd 表示文件描述符,如果不写,默认为 0,也就是标准输入文件。

举例:
Linux wc 命令可以用来对文本进行统计,包括单词个数、行数、字节数
相关参数:
-c 统计字节数
-w 统计单词数
-l 统计行数

举例:
(1)求出文件中有多少行文本:

[root@admin world]# wc -l < test02
22

(2)将文件中的所有文本输出:
注意:while read str后是;分号

#写一个shell脚本
while read str; do
    echo $str
done <test02

(3)统计用户在终端输入的文本行数:
EOF 分界符:
在输入分界符时必须一模一样,不能缩写

[root@admin world]# wc -l <<unny
> 123
> 43234
> 345
> 645
> 674
> 57
> 
> 4565
> 464
> 56
> 45
> 6
> 645
> 345
> 3234
> Bunny
> unny
16
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值