前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【趣学程序】Linux流的重定向

【趣学程序】Linux流的重定向

作者头像
趣学程序-shaofeer
发布2019-07-27 18:31:31
7240
发布2019-07-27 18:31:31
举报
文章被收录于专栏:upuptop的专栏upuptop的专栏

获取当前bash窗口的进程id

代码语言:javascript
复制
[root@CentOS6 shell]# echo $$1586

查看当前进行的所有进程id

代码语言:javascript
复制
[root@CentOS6 shell]# ps -efUID        PID  PPID  C STIME TTY          TIME CMDroot         1     0  0 08:22 ?        00:00:01 /sbin/initroot         2     0  0 08:22 ?        00:00:00 [kthreadd]root         3     2  0 08:22 ?        00:00:00 [migration/0]

执行shell脚本的方式

  1. 创建文件test.txt
  2. 编辑源代码
代码语言:javascript
复制
[root@CentOS6 shell]# vim test.txt
echo 'HelloWorld'ls -l /echo $$
  1. 运行test.txt文件
代码语言:javascript
复制
第一种方式:使用source方式:
[root@CentOS6 shell]# source test.txtHelloWorld总用量 98dr-xr-xr-x.   2 root root  4096 10月 12 2018 bindr-xr-xr-x.   5 root root  1024 10月 10 2018 bootdrwxr-xr-x.   2 root root  4096 11月 23 2013 cgroupdrwxr-xr-x.  19 root root  3760 7月  16 08:22 devdrwxr-xr-x. 103 root root 12288 7月  16 08:22 etcdrwxr-xr-x.   2 root root  4096 9月  23 2011 homedr-xr-xr-x.  18 root root 12288 10月 12 2018 libdrwx------.   2 root root 16384 10月 10 2018 lost+founddrwxr-xr-x.   2 root root  4096 9月  23 2011 mediadrwxr-xr-x.   2 root root     0 7月  16 08:22 miscdrwxr-xr-x.   2 root root  4096 9月  23 2011 mntdrwxr-xr-x.   2 root root     0 7月  16 08:22 netdrwxr-xr-x.   3 root root  4096 10月 10 2018 optdr-xr-xr-x.  97 root root     0 7月  16 08:22 procdr-xr-x---.   3 root root  4096 7月  16 08:27 rootdr-xr-xr-x.   2 root root 12288 10月 12 2018 sbindrwxr-xr-x.   7 root root     0 7月  16 08:22 selinuxdrwxr-xr-x.   2 root root  4096 9月  23 2011 srvdrwxr-xr-x.  13 root root     0 7月  16 08:22 sysdrwxrwxrwt.   3 root root  4096 7月  16 08:22 tmpdrwxr-xr-x.  13 root root  4096 10月 11 2018 usrdrwxr-xr-x.  21 root root  4096 10月 11 2018 var1586
第二种方式使用 .[root@CentOS6 shell]#  . test.txt
第三种方式使用 /bin/bash test.txt[root@CentOS6 shell]# /bin/bash test.txtHelloWorld总用量 98dr-xr-xr-x.   2 root root  4096 10月 12 2018 bindr-xr-xr-x.   5 root root  1024 10月 10 2018 bootdrwxr-xr-x.   2 root root  4096 11月 23 2013 cgroupdrwxr-xr-x.  19 root root  3760 7月  16 08:22 devdrwxr-xr-x. 103 root root 12288 7月  16 08:22 etcdrwxr-xr-x.   2 root root  4096 9月  23 2011 homedr-xr-xr-x.  18 root root 12288 10月 12 2018 libdrwx------.   2 root root 16384 10月 10 2018 lost+founddrwxr-xr-x.   2 root root  4096 9月  23 2011 mediadrwxr-xr-x.   2 root root     0 7月  16 08:22 miscdrwxr-xr-x.   2 root root  4096 9月  23 2011 mntdrwxr-xr-x.   2 root root     0 7月  16 08:22 netdrwxr-xr-x.   3 root root  4096 10月 10 2018 optdr-xr-xr-x.  98 root root     0 7月  16 08:22 procdr-xr-x---.   3 root root  4096 7月  16 08:30 rootdr-xr-xr-x.   2 root root 12288 10月 12 2018 sbindrwxr-xr-x.   7 root root     0 7月  16 08:22 selinuxdrwxr-xr-x.   2 root root  4096 9月  23 2011 srvdrwxr-xr-x.  13 root root     0 7月  16 08:22 sysdrwxrwxrwt.   3 root root  4096 7月  16 08:22 tmpdrwxr-xr-x.  13 root root  4096 10月 11 2018 usrdrwxr-xr-x.  21 root root  4096 10月 11 2018 var1636

总结

  1. 使用 source或者 .进行执行文本文本是在current shell中执行的,即为当前shell
  2. 使用 /bin/bash 执行文本文件,是启动了一个新的子进程,执行了文本文件,然后退出了子进程
  3. 每次都需要写 /bin/bash去执行文件比较麻烦,所以可以将该命令写于文件内部的第一行。

text.txt:

代码语言:javascript
复制
#/bin/bashecho 'HelloWorld'ls -l /echo $$

赋予可执行的权限[root@CentOS6 shell]#chmod+x test.txt直接执行文件、

代码语言:javascript
复制
[root@CentOS6 shell]# ./test.txtHelloWorld总用量 98dr-xr-xr-x.   2 root root  4096 10月 12 2018 bindr-xr-xr-x.   5 root root  1024 10月 10 2018 bootdrwxr-xr-x.   2 root root  4096 11月 23 2013 cgroupdrwxr-xr-x.  19 root root  3760 7月  16 08:22 devdrwxr-xr-x. 103 root root 12288 7月  16 08:22 etcdrwxr-xr-x.   2 root root  4096 9月  23 2011 homedr-xr-xr-x.  18 root root 12288 10月 12 2018 libdrwx------.   2 root root 16384 10月 10 2018 lost+founddrwxr-xr-x.   2 root root  4096 9月  23 2011 mediadrwxr-xr-x.   2 root root     0 7月  16 08:22 miscdrwxr-xr-x.   2 root root  4096 9月  23 2011 mntdrwxr-xr-x.   2 root root     0 7月  16 08:22 netdrwxr-xr-x.   3 root root  4096 10月 10 2018 optdr-xr-xr-x. 100 root root     0 7月  16 08:22 procdr-xr-x---.   3 root root  4096 7月  16 08:34 rootdr-xr-xr-x.   2 root root 12288 10月 12 2018 sbindrwxr-xr-x.   7 root root     0 7月  16 08:22 selinuxdrwxr-xr-x.   2 root root  4096 9月  23 2011 srvdrwxr-xr-x.  13 root root     0 7月  16 08:22 sysdrwxrwxrwt.   3 root root  4096 7月  16 08:22 tmpdrwxr-xr-x.  13 root root  4096 10月 11 2018 usrdrwxr-xr-x.  21 root root  4096 10月 11 2018 var1668
proc目录

proc目录是linux将所有的进程通过文件的形式来进行表示了,我们的shell窗口是1668,就有1668这样一个文件夹

代码语言:javascript
复制
[root@CentOS6 proc]# cd $$[root@CentOS6 1586]# pwd/proc/1586
[root@CentOS6 1586]# lsattr       cgroup      comm             cwd      fd      limits    mem        mountstats  oom_adj        pagemap      sched      smaps  statm    taskautogroup  clear_refs  coredump_filter  environ  fdinfo  loginuid  mountinfo  net         oom_score      personality  schedstat  stack  status   wchanauxv       cmdline     cpuset           exe      io      maps      mounts     ns          oom_score_adj  root         sessionid  stat   syscall
fd目录解释:文件描述符,每一个进程都会有。
[root@CentOS6 1586]# cd fd[root@CentOS6 fd]# pwd/proc/1586/fd[root@CentOS6 fd]# ll总用量 0lrwx------. 1 root root 64 7月  16 08:23 0 -> /dev/pts/0lrwx------. 1 root root 64 7月  16 08:39 1 -> /dev/pts/0lrwx------. 1 root root 64 7月  16 08:25 2 -> /dev/pts/0lrwx------. 1 root root 64 7月  16 08:39 255 -> /dev/pts/0
## 每个进程都会有文件0,1,2文件0 -> 标准输入流文件1 -> 标准输出流文件2 -> 错误输出流
每个流都指向:/dev/pts/0
当我们在开一个窗口的时候,就会多一个文件夹:/dev/pts/1
相当于我们在窗口1操作交互时,标准输入流是从/dev/pts/0接入的,标准输出流是指向/dev/pts/0的。相当于我们在窗口2操作交互时,标准输入流是从/dev/pts/1接入的,标准输出流是指向/dev/pts/1的。

流的重定向

如何通过在左边窗口输入代码,在右边输出结果呢?

重定向语法:

代码语言:javascript
复制
#创建一个新的流指向1 相当于备份1的指向exec 6>&1 
#更新一个流的指向位置 这里执行了第二个窗口exec 1> /dev/pts/1

代码实操:

通过上面的代码设置,就实现了左边窗口输入,右边窗口输出的需求

通过命令 [root@CentOS6 fd]#exec1>&6就可以恢复了

重定向常用

重定向输出流

通过重定向可以改变你程序的输出位置

代码语言:javascript
复制
1.重定向标准输出流到文件(不追加)[root@CentOS6 shell]# ll 1> a.log[root@CentOS6 shell]# cat a.log总用量 4-rw-r--r--. 1 root root  0 7月  16 08:53 a.log-rwxr-xr-x. 1 root root 45 7月  16 08:34 test.txt
2.重定向标准输出流到文件(追加)[root@CentOS6 shell]# cat a.log总用量 4-rw-r--r--. 1 root root  0 7月  16 08:53 a.log-rwxr-xr-x. 1 root root 45 7月  16 08:34 test.txt总用量 8-rw-r--r--. 1 root root 111 7月  16 08:53 a.log-rwxr-xr-x. 1 root root  45 7月  16 08:34 test.txt
3.重定向标准输出流到文件(省略 1 )[root@CentOS6 shell]# ll > a.log[root@CentOS6 shell]# cat a.log总用量 4-rw-r--r--. 1 root root  0 7月  16 08:55 a.log-rwxr-xr-x. 1 root root 45 7月  16 08:34 test.txt
4.重定向错误输出[root@CentOS6 shell]# cd aaaaa-bash: cd: aaaaa: 没有那个文件或目录[root@CentOS6 shell]# cd aaaa 2> error.log[root@CentOS6 shell]# cat error.log-bash: cd: aaaa: 没有那个文件或目录
5.将错误和正确的保存到两个文件[root@CentOS6 shell]# ll /home/ /gogd 1>a.log 2>b.log[root@CentOS6 shell]# cat a.log/home/:总用量 0[root@CentOS6 shell]# cat b.logls: 无法访问/gogd: 没有那个文件或目录
6.指向一个文件  ,先让2指向文件,然后让1指向2[root@CentOS6 shell]# ll /root/ /gogo 2>b.log  1>&2 [root@CentOS6 shell]# cat b.logls: 无法访问/gogo: 没有那个文件或目录/root/:总用量 48-rw-------. 1 root root  1436 10月 10 2018 anaconda-ks.cfg-rw-r--r--. 1 root root 26150 10月 10 2018 install.log-rw-r--r--. 1 root root  7764 10月 10 2018 install.log.syslogdrwxr-xr-x. 2 root root  4096 7月  16 08:57 shell
7.简单写法 >&
[root@CentOS6 shell]# ll /root/ /gogo >& b.log[root@CentOS6 shell]# cat b.logls: 无法访问/gogo: 没有那个文件或目录/root/:总用量 48-rw-------. 1 root root  1436 10月 10 2018 anaconda-ks.cfg-rw-r--r--. 1 root root 26150 10月 10 2018 install.log-rw-r--r--. 1 root root  7764 10月 10 2018 install.log.syslogdrwxr-xr-x. 2 root root  4096 7月  16 08:57 shell
重定向输入流
代码语言:javascript
复制
1.使用read向某一个变量赋值,会阻塞等待输入回车符[root@CentOS6 shell]# read nameupuptop[root@CentOS6 shell]# echo $nameupuptop
2.使用三个 < 可以加一个字符串[root@CentOS6 shell]# read name 0<<<"upuptopupuptop"[root@CentOS6 shell]# echo $nameupuptopupuptop
3.使用两个 < 可以添加一个结束符,多行输入,等待输入结束符,但是read读到换行符就结束了[root@CentOS6 shell]# read name 0<<OO> hello> world> hah> OO[root@CentOS6 shell]# echo $namehello4.使用一个 < 可以从文件中读取[root@CentOS6 shell]# cat error.log-bash: cd: aaaa: 没有那个文件或目录[root@CentOS6 shell]# read name 0<error.log[root@CentOS6 shell]# echo $name-bash: cd: aaaa: 没有那个文件或目录
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 趣学程序 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取当前bash窗口的进程id
  • 查看当前进行的所有进程id
  • 执行shell脚本的方式
    • proc目录
    • 流的重定向
    • 重定向常用
      • 重定向输出流
        • 重定向输入流
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档