重定向作用

一个进程默认会打开标准输入、标准输出、错误输出三个文件描述符。

重定向可以让我们的程序的标准输出、错误输出的信息重定向文件里,那么这里还可以将文件的内容代替键盘作为一种标准输入的方式。


重定向符号

  • 输入重定向符号"<"

  • 输出重定向符号">",">>","2>","&>"


输入重定向功能

01 输入重定向符号"<"的作用:

会把文件的内容当做参数输入到进程,如下例子:

[root@omp120 home]# cat file.txt
hello
[root@omp120 home]# read a < file.txt
[root@omp120 home]# echo $a
hello

file.txt文件的内容是hello,上述的例子就是把file.txt的内容重定向到a这个变量,并把a变量打印出来。


输出重定向功能

01 输出重定向符号">"的作用:

会把文件内容清空,在把输出内容重定向到指定的文件里,并且如果文件不存在则创建,如下例子:

[root@lincoding tmp]# echo 123 > /tmp/test
[root@lincoding tmp]# cat /tmp/test
123
[root@lincoding tmp]# echo abc > /tmp/test
[root@lincoding tmp]# cat /tmp/test
abc
02 输出重定向符号">>"的作用:

会把输出的内容追加到指定的文件里,该文件不会被清空,并且如果文件不存在则创建,如下例子:

[root@lincoding tmp]# echo 123 >> /tmp/test
[root@lincoding tmp]# cat /tmp/test
123
[root@lincoding tmp]# echo abc >> /tmp/test
[root@lincoding tmp]# cat /tmp/test
123
abc
03 输出重定向符号"2>"的作用:

是把进程错误输出的内容重定向到指定的文件里,如下例子:

[root@lincoding home]# abc
-bash: abc: command not found
[root@lincoding home]# abc > error.txt
-bash: abc: command not found
[root@lincoding home]# cat error.txt
[root@lincoding home]#
[root@lincoding home]# abc 2> error.txt
[root@lincoding home]# cat error.txt
-bash: abc: command not found

以上的演示结果可以得知,abc不是Linux的命令,执行了会报错说abd命令未找到的错误信息输出,那么这个错误信息需要用2>重定向符才能把进程错误输出的内容重定向到指定的文件。

04 输出重定向符号"&>"的作用:

无论进程输出的信息是正确还是错误的信息,都会重定向到指定的文件里,如下例子:

[root@lincoding home]# abc &> file.txt
[root@lincoding home]# cat file.txt
-bash: abc: command not found
[root@lincoding home]# free -m &> file.txt
[root@lincoding home]# cat file.txt
total used free shared buffers cached
Mem: 980 918 62 0 71 547
-/+ buffers/cache: 299 681
Swap: 1983 0 1983

输入重定向和输出重定向组合使用

输入和输出也是可以组合使用的,那么这个组合主要应用于在Shell脚本当中产生新的配置文件的场景,如下Shell脚本例子:

#!/bin/bash
cat > /home/a.sh << EOF
echo "hello bash"
EOF

cat命令的输出重定向到/root/a.sh脚本文件,并且用输入重定向把EOF为脚本结尾。那么通过执行这个脚本,就会产生一个内容为echo "hello bash"文件名为a.sh的脚本文件。

执行结果:

[root@lincoding home]# ./test.sh
[root@lincoding home]# ls -l a.sh
-rw-r--r--. 1 root root 18 Sep 27 16:41 a.sh
[root@lincoding home]# chmod u+x a.sh
[root@lincoding home]# cat a.sh
echo "hello bash"
[root@lincoding home]# ./a.sh
hello bash

小结

以上的内容就是关于输入和输出重定向的用法,那么大家要注意输出重定向包括覆盖和追加模式,无论是覆盖还是追加模式,尽量不要用于我们的系统配置文件,那么在应用之前大家要注意对系统文件进行备份。

输入和输出重定向,还可以组合使用,一般在Shell脚本当中去产生新的配置文件的时候,会用到它们的组合的方式。


最新文章

  1. RTC时钟
  2. VMware 关闭虚拟机 Ubuntu 12 的 3D 效果,提高性能
  3. Android 首页图片轮播
  4. javascript function new this
  5. NLTk
  6. Linux Kconfig及Makefile学习
  7. hostname
  8. 11个让你吃惊的Linux终端命令
  9. 第五篇:python高级之面向对象高级
  10. CSS3之背景色渐变
  11. ACM/ICPM2014鞍山现场赛D Galaxy (HDU 5073)
  12. JavaScript变量存储浅析
  13. [HTTP] PHP 实现 HTTP Server 原理
  14. angular5 组件通信(一)
  15. CSS知识点总结[部分]
  16. Spark记录-SparkSQL远程操作MySQL和ORACLE
  17. 链接SQL Server服务器
  18. Win10系列:JavaScript动画4
  19. c# winform窗体边框风格的设计
  20. 【Android】17.0 UI开发(八)——利用RecyclerView列表控件实现精美的聊天界面

热门文章

  1. 从《华为的冬天》到AI的冬天 | 甲子光年
  2. 顺F速运,你被爱加M坑了
  3. iOS9新框架—Watch Connectivity(详情:http://ios.itcast.cn/subject/ios9/index.shtml )
  4. Python用Pandas读写Excel
  5. MySQL基础之数据管理【3】
  6. Web服务器—IIS
  7. debian 10 安装fcitx 后设置
  8. Tensorflow之多元线性回归问题(以波士顿房价预测为例)
  9. aa:function()和function aa()
  10. C++ 标准库 std::find 查找