460-Linux基础(输入输出重定向<、<< 、>、>>)

1、简介

Linux 中标准输入设备:默认指的是键盘

标准输出设备:默认指的是显示器

输入重定向:指的是重新指定设备代替键盘作为新的输入设备
输出重定向:指的是重新指定设备代替显示器作为新的输出设备

通常是用文件或命令的执行结果代替键盘作为新的输入设备,而新的输出设备通常指的就是文件

2、Linux输入重定向

输入重定向来说,其需要用到的符号以及作用如表 1 所示。

在这里插入图片描述
例1:
默认情况下,cat 命令会接受标准输入设备(键盘)的输入,并显示到控制台,但如果用文件代替键盘作为输入设备,那么该命令会以指定的文件作为输入设备,并将文件中的内容读取并显示到控制台。

以 /etc/passwd 文件(存储了系统中所有用户的基本信息)为例,执行如下命令:

nowcoder@nowcoder:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
nowcoder@nowcoder:~$ cat < /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...

注意:

  • 虽然执行结果相同,但第一个代表是以键盘作为输入设备,而第二个代码是以 /etc/passwd 文件作为输入设备。

例2:

[root@localhost ~]# cat << 0
>c.biancheng.net
>Linux
>0
c.biancheng.net
Linux

指定了 0 作为分界符之后,只要不输入 0,就可以一直输入数据。


例3:

首先,新建文本文件 a.tx,然后执行如下命令:

[root@localhost ~]# cat a.txt
[root@localhost ~]# cat < /etc/passwd > a.txt
[root@localhost ~]# cat a.txt
#输出了和 /etc/passwd 文件内容相同的数据

通过重定向 /etc/passwd 作为输入设备,并输出重定向到 a.txt,最终实现了将 /etc/passwd 文件中内容复制到 a.txt 中。

3、Linux输出重定向

输出重定向还可以细分为标准输出重定向错误输出重定向

例如,使用 ls 命令分别查看两个文件的属性信息,但其中一个文件是不存在的,如下所示:

[root@localhost ~]# touch demo1.txt
[root@localhost ~]# ls -l demo1.txt
-rw-rw-r--. 1 root root 0 Oct 12 15:02 demo1.txt
[root@localhost ~]# ls -l demo2.txt    # demo2.txt不存在的文件
ls: cannot access demo2.txt: No such file or directory
  • demo1.txt 是存在的,因此正确输出了该文件的一些属性信息,这也是该命令执行的标准输出信息;

  • demo2.txt 是不存在的,因此执行 ls 命令之后显示的报错信息,是该命令的错误输出信息。

再次强调,要想把原本输出到屏幕上的数据转而写入到文件中,这两种输出信息就要区别对待。

在此基础上,标准输出重定向错误输出重定向又分别包含清空写入追加写入两种模式。

因此,对于输出重定向来说,其需要用到的符号以及作用如表 2 所示。

在这里插入图片描述
举例:新建一个包含有 “Linux” 字符串的文本文件 Linux.txt,以及空文本文件 demo.txt;

然后执行如下命令

[root@localhost ~]# cat Linux.txt > demo.txt
[root@localhost ~]# cat demo.txt
Linux
[root@localhost ~]# cat Linux.txt > demo.txt
[root@localhost ~]# cat demo.txt
Linux     <--这里的 Linux 是清空原有的 Linux 之后,写入的新的 Linux
[root@localhost ~]# cat Linux.txt >> demo.txt
[root@localhost ~]# cat demo.txt
Linux
Linux     <--以追加的方式,新数据写入到原有数据之后
[root@localhost ~]# cat b.txt 2> demo.txt
[root@localhost ~]# cat demo.txt
cat: b.txt: No such file or directory  <--清空文件,再将错误输出信息写入到该文件中
[root@localhost ~]# cat b.txt 2>> demo.txt
[root@localhost ~]# cat demo.txt
cat: b.txt: No such file or directory
cat: b.txt: No such file or directory  <--追加写入错误输出信息
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux 中使用 C 语言实现 shell 命令 <> 的重定向,可以使用 dup2() 函数来实现。dup2() 函数可以将一个文件描述符的内容复制到另一个文件描述符中,从而实现文件重定向。 下面是一个简单的示例程序,可以实现 “<” 和 “>” 的文件重定向功能: ```c #include <stdio.h> #include <unistd.h> #include <fcntl.h> int main(int argc, char *argv[]) { int in_fd, out_fd; char *in_file, *out_file, *cmd; if (argc != 4) { fprintf(stderr, "Usage: %s <input_file> <output_file> <command>\n", argv[0]); return 1; } in_file = argv[1]; out_file = argv[2]; cmd = argv[3]; // 打开输入文件 in_fd = open(in_file, O_RDONLY); if (in_fd == -1) { perror("open input file"); return 2; } // 打开输出文件 out_fd = open(out_file, O_CREAT|O_WRONLY|O_TRUNC, 0644); if (out_fd == -1) { perror("open output file"); return 2; } // 将输入文件重定向到标准输入 if (dup2(in_fd, STDIN_FILENO) == -1) { perror("dup2 input file"); return 3; } // 将输出文件重定向到标准输出 if (dup2(out_fd, STDOUT_FILENO) == -1) { perror("dup2 output file"); return 3; } // 执行命令 if (system(cmd) == -1) { perror("system command"); return 4; } // 关闭文件 close(in_fd); close(out_fd); return 0; } ``` 在上面的程序中,我们首先打开输入文件和输出文件,并将它们分别重定向到标准输入和标准输出。然后执行传入的命令,命令的输出将被重定向到输出文件中。最后关闭文件描述符。 使用该程序可以实现以下命令的效果: ```bash $ ./redirect input.txt output.txt wc -l ``` 该命令会将输入文件 input.txt 的内容通过 “<” 重定向到 wc 命令的标准输入中,将 wc 的输出通过 “>” 重定向到 output.txt 文件中。最后输出文件 output.txt 中行数的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufeng2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值