linux 从入门到跑路
重定向 管道
Linux给程序提供三种I/O设备
标准输入(STDIN)-0默认接受来自键盘的输入
标准输出(STDOUT)-1默认输出到终端窗口
标准错误(STDERR)-2默认输出到终端窗口
I/O重定向:改变默认位置
重定向
重定向会覆盖原文件内容
> 把STDOUT重定向到文件
2> 把STDERR重定向到文件
&> 把所有输出重定向到文件
set –C 禁止将内容覆盖已有文件,但可追加
set +C 允许覆盖
>| file 强制覆盖
#多输出实例 [root@localhost ~]# ll /mytext/ 总用量 0 -rwxrwxrwx. 1 root root 0 7月 22 03:52 1 -rw-r--r--. 1 root root 0 7月 22 03:52 10 -rwxrwxrwx. 1 root root 0 7月 22 03:52 2 -rwxrwxrwx. 1 root root 0 7月 22 03:52 3 -rwxrwxrwx. 1 root root 0 7月 22 03:52 4 -rwxrwxrwx. 1 root root 0 7月 22 03:52 5 -rwxrwx---. 1 root root 0 7月 22 03:52 6 -rwxrwx---. 1 root root 0 7月 22 03:52 7 -rwxrwx---. 1 root root 0 7月 22 03:52 8 -rwxrwx---. 1 root root 0 7月 22 03:52 9 [francis@localhost ~]$ cp -vr /mytext/ /app/hello 1>correct.txt 2>error.txt [francis@localhost ~]$ cat correct.txt "/mytext/" -> "/app/hello/mytext" "/mytext/1" -> "/app/hello/mytext/1" "/mytext/2" -> "/app/hello/mytext/2" "/mytext/3" -> "/app/hello/mytext/3" "/mytext/4" -> "/app/hello/mytext/4" "/mytext/5" -> "/app/hello/mytext/5" "/mytext/6" -> "/app/hello/mytext/6" "/mytext/7" -> "/app/hello/mytext/7" "/mytext/8" -> "/app/hello/mytext/8" "/mytext/9" -> "/app/hello/mytext/9" "/mytext/10" -> "/app/hello/mytext/10" [francis@localhost ~]$ cat error.txt cp: 无法打开"/mytext/6" 读取数据: 权限不够 cp: 无法打开"/mytext/7" 读取数据: 权限不够 cp: 无法打开"/mytext/8" 读取数据: 权限不够 cp: 无法打开"/mytext/9" 读取数据: 权限不够 [francis@localhost ~]$ cp -vr /mytext/ /app/hello 1>correct.txt 2>error.txt &> both.txt [francis@localhost ~]$ cat both.txt "/mytext/1" -> "/app/hello/mytext/1" "/mytext/2" -> "/app/hello/mytext/2" "/mytext/3" -> "/app/hello/mytext/3" "/mytext/4" -> "/app/hello/mytext/4" "/mytext/5" -> "/app/hello/mytext/5" "/mytext/6" -> "/app/hello/mytext/6" cp: 无法打开"/mytext/6" 读取数据: 权限不够 "/mytext/7" -> "/app/hello/mytext/7" cp: 无法打开"/mytext/7" 读取数据: 权限不够 "/mytext/8" -> "/app/hello/mytext/8" cp: 无法打开"/mytext/8" 读取数据: 权限不够 "/mytext/9" -> "/app/hello/mytext/9" cp: 无法打开"/mytext/9" 读取数据: 权限不够 "/mytext/10" -> "/app/hello/mytext/10"
重定向的输入输出解析
<为输入符号,无论位置在哪,都会优先执行将待输入文件输入到命令中
>为输出符号无论位置在哪,都会最后执行将命令的处理结果输出到指定文件中
[francis@localhost ~]$ tr a-z A-Z < /etc/issue > /tmp/issue.out [francis@localhost ~]$ cat /tmp/issue.out S KERNEL R ON AN M [francis@localhost ~]$ tr a-z A-Z > /tmp/issue.out < /etc/issue [francis@localhost ~]$ cat /tmp/issue.out S KERNEL R ON AN M
以上两条命令结果相同,因为执行顺序为 输入 ->待接收的命令->输出
追加重定向
>> 原有内容基础上,追加内容,而不是直接覆盖文件