1、输入重定向:标准的输入设备是键盘,常规的输入途径通过键盘进行信息的输入。输入重定向就是不使用系统提供的标准输入端口,重设读取数据的方式。例如,输入重定向为一个文件,系统就会从文件中读取数据进行运作,而不是通过键盘录入数据让系统进行读取。
2、标准输入文件(stdin):stdin 的文件描述符为0,默认输入硬件 ---- 键盘。
3、输入重定向的符号是:< (小于号)
4、输入重定向的格式与说明:
-
command <file:重定向 file 文件中的内容作为标准输入, command 执行 filie 的内容。
-
command <<END:从标准输入(键盘)中输入数据。END 是结束符,当输入 END 的时候停止输入操作。(结束符可以是任意的字符串,用户自己定义)。
-
command <file1 >file2 输入输出重定向, file1 作为标准输入, command 执行 file1的内容,然后把结果输出到 file2。
5、实操练习:
- ①、command <file:重定向 file 文件中的内容作为标准输入, command 执行 filie 的内容。
[root@localhost ~]#
[root@localhost ~]# ll ## 现时有一个文件 backups.txt
total 4
-rw-r--r--. 1 root root 176 Oct 21 09:33 backups.txt
[root@localhost ~]#
## 重定向输入为 backups.txt,cat 遍历 backups.txt 的内容
[root@localhost ~]# cat <backups.txt
hello
-bash: abc: command not found
ls: cannot access 123: No such file or directory
hello world
-bash: efg: command not found
ls: cannot access 456: No such file or directory
[root@localhost ~]#
## 重定向输入为 backups.txt,wc -l 遍历 backups.txt 的内容,统计有多少行
[root@localhost ~]# wc -l <backups.txt
6
## 用 wc -l 命令统计 backups.txt 和 重定向输入 backups.txt 后用 wc -l 命令统计的区别
## wc -l 命令统计 backups.txt 文件会显示行数 和 文件名
## 重定向输入 backups.txt 后用 wc -l 命令统计,只会显示行号。
## 因为 重定向输入的 backups.txt 只作为输入方,命令只会执行里面的内容,而不是作为执行一个文件。
[root@localhost ~]# wc -l backups.txt
6 backups.txt
[root@localhost ~]#
- ②、command <<END:从标准输入(键盘)中输入数据。END 是结束符,当输入 END 的时候停止输入操作。(结束符可以是任意的字符串,用户自己定义)。
- cat 打印键盘输入的内容。
[root@localhost ~]#
[root@localhost ~]# cat <<END ## 标准输入(键盘)内容,cat 打印内容
> 1
> 2
> 3
> 4
> 5
> END
1
2
3
4
5
[root@localhost ~]#
- wc -l 统计键盘输入的内容有多少行。
[root@localhost ~]# wc -l << end
> 1
> 2
> 3
> end
3 ## 3行内容
[root@localhost ~]#
- ③、command <file1 >file2 输入输出重定向, file1 作为标准输入, command 执行 file1的内容,然后把结果输出到 file2。
[root@localhost ~]# vim catFile.txt ## vim 编辑一个 catFile.txt 文件并输入内容
Hello
World
~
~
~
:wq ## 保存退出
## cat 读取输入重定向的 catFile.txt,并把结果输出重定向到 showCatFile.txt 文件
[root@localhost ~]# cat <catFile.txt >showCatFile.txt
[root@localhost ~]#
[root@localhost ~]# cat showCatFile.txt ## showCatFile.txt 文件内容
Hello
World
[root@localhost ~]#
## wc -l 统计输入重定向的 catFile.txt,并把结果输出重定向到 showWcFile.txt 文件
[root@localhost ~]# wc -l <catFile.txt >showWcFile.txt
[root@localhost ~]#
[root@localhost ~]# cat showWcFile.txt
2 ## 统计出有两行内容
[root@localhost ~]#
- bc 计算输入重定向的文件 result.txt,把结果重定向输出到 showResult.txt。
[root@localhost ~]# vim result.txt ## 编辑 result.txt,输入需要计算的内容
1+2
4-3
3*3
1/1
~
~
~
:wq ## 保存并退出
## bc 计算输入重定向的 result.txt,并把结果输出重定向到 showResult.txt 文件
[root@localhost ~]# bc <result.txt >showResult.txt
[root@localhost ~]#
[root@localhost ~]# cat showResult.txt ## 计算结果
3
1
9
1
[root@localhost ~]#
- ④、当指令执行键盘输入的内容后,再重定向输出到文件。屏幕不会显示输入时的内容,而是把指令执行的结果输出到指定文件。
[root@localhost ~]# cat <<end >file.txt
> 1
> 2
> 3
> 4
> 5
> end ##/ <---- 收到结束符后不会打印输入的信息,而是重定向输出到 file.txt 文件
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root 176 Oct 21 09:33 backups.txt
-rw-r--r--. 1 root root 10 Oct 21 10:44 file.txt
[root@localhost ~]#
[root@localhost ~]# cat <file.txt ## file.txt 的内容
1
2
3
4
5
[root@localhost ~]#
[root@localhost ~]# wc -l <<end >file.txt
> hello
> world
> end ##/ <---- 收到结束符后不会打印输入信息有多少行,而是把答案重定向输出到 file.txt 文件
[root@localhost ~]#
[root@localhost ~]# ll
total 8
-rw-r--r--. 1 root root 176 Oct 21 09:33 backups.txt
-rw-r--r--. 1 root root 2 Oct 21 10:48 file.txt
[root@localhost ~]#
[root@localhost ~]# cat <file.txt ## wc -l 统计的行数
2
[root@localhost ~]#
网友评论