4-15 Linux中的输入重定向

2021-12-12  本文已影响0人  捌千里路雲和月

1、输入重定向:标准的输入设备是键盘,常规的输入途径通过键盘进行信息的输入。输入重定向就是不使用系统提供的标准输入端口,重设读取数据的方式。例如,输入重定向为一个文件,系统就会从文件中读取数据进行运作,而不是通过键盘录入数据让系统进行读取。

2、标准输入文件(stdin):stdin 的文件描述符为0,默认输入硬件 ---- 键盘。

3、输入重定向的符号是:< (小于号)

4、输入重定向的格式与说明:

5、实操练习:

[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 ~]# 

[root@localhost ~]# 
[root@localhost ~]# cat <<END    ## 标准输入(键盘)内容,cat 打印内容
> 1
> 2
> 3
> 4
> 5
> END
1
2
3
4
5
[root@localhost ~]# 

[root@localhost ~]# wc -l << end
> 1
> 2
> 3
> end
3    ## 3行内容
[root@localhost ~]# 

[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 ~]# 
                          
[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 ~]# 

上一篇下一篇

猜你喜欢

热点阅读