重定向

  • 输入输出
    • 标准输入
    • 标准输出 1
    • 标准错误输出 2
  • 输出重定向
    • >覆盖重定向
    • >>追加重定向
    • 2>覆盖重定向错误输出流
    • 2>>追加重定向错误输出流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@test tmp]# echo "hello kinmfer"
hello eagles
[root@test tmp]# echo "hello kinmfer" > 2.txt
[root@test tmp]# cat 2.txt
hello eagles
[root@test tmp]# echo "hello kinmfer" >> 2.txt
[root@test tmp]# cat 2.txt
hello eagles
hello eagles
[root@test tmp]# lnd
-bash: lnd: command not found
[root@test tmp]# lnd 2> output_error
[root@test tmp]# cat output_error
-bash: lnd: command not found
[root@test tmp]# lnd 2>> output_error
[root@test tmp]# cat output_error
-bash: lnd: command not found
-bash: lnd: command not found
  • 标准输出和错误输出到各自不同的位置
1
2
3
4
5
6
7
8
9
10
11
12
[root@test tmp]# ll 1> file.out 2> file_error.out
[root@test tmp]# cat file.out
total 8
lrwxrwxrwx. 1 root root 5 Feb 6 07:26 1_soft.txt -> 1.txt
-rw-r--r--. 1 root root 0 Feb 6 07:26 1.txt
-rw-r--r--. 1 root root 26 Feb 6 07:51 2.txt
-rw-r--r--. 1 root root 0 Feb 6 07:53 file_error.out
-rw-r--r--. 1 root root 0 Feb 6 07:53 file.out
-rw-r--r--. 1 root root 60 Feb 6 07:51 output_error
[root@test tmp]# lld 1> file.out 2> file_error.out
[root@test tmp]# cat file_error.out
-bash: lld: command not found
  • 垃圾桶: /dev/null 被重定向到此的会被清空

  • 合并标准输出和错误输出为同一数据流进行重定向

    • &> 覆盖重定向
    • &>> 追加重定向
    • 2>&1 将错误流重定向到标准输出文件中 (>>)
    • 1>&2 将正确流重定向到错误输出文件中 (>>)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@test tmp]# lld &> all.out
[root@test tmp]# ll &>> all.out
[root@test tmp]# cat all.out
-bash: lld: command not found
total 16
lrwxrwxrwx. 1 root root 5 Feb 6 07:26 1_soft.txt -> 1.txt
-rw-r--r--. 1 root root 0 Feb 6 07:26 1.txt
-rw-r--r--. 1 root root 26 Feb 6 07:51 2.txt
-rw-r--r--. 1 root root 30 Feb 6 07:56 all.out
-rw-r--r--. 1 root root 30 Feb 6 07:53 file_error.out
-rw-r--r--. 1 root root 0 Feb 6 07:53 file.out
-rw-r--r--. 1 root root 60 Feb 6 07:51 output_error

[root@test tmp]# lld 1> file.out 2> file_error.out 2>&1
[root@test tmp]# cat file.out
-bash: lld: command not found
[root@test tmp]# ll 1> file.out 2> file_error.out 1>&2
[root@test tmp]# cat file_error.out
total 16
-rw-r--r--. 1 root root 30 Feb 6 07:57 1.out
lrwxrwxrwx. 1 root root 5 Feb 6 07:26 1_soft.txt -> 1.txt
-rw-r--r--. 1 root root 0 Feb 6 07:26 1.txt
-rw-r--r--. 1 root root 26 Feb 6 07:51 2.txt
-rw-r--r--. 1 root root 396 Feb 6 07:56 all.out
-rw-r--r--. 1 root root 0 Feb 6 07:59 file_error.out
-rw-r--r--. 1 root root 0 Feb 6 07:59 file.out
-rw-r--r--. 1 root root 60 Feb 6 07:51 output_error
  • 标准输入

    • 覆盖:

      1
      2
      3
      4
      cat > /path/somefile <<EOF
      ....

      EOF
    • 追加:

      1
      2
      3
      cat >> /path/somefile <<EOF
      ....
      EOF
  • tee命令

    tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件 。

    • -a 追加
    • -i 无视中断

管道

command1 | command2 | command3 ...
最后一个命令会在当前shell进程的子shell进程中执行;前一个命令的执行结果会由当前命令继续使用。

  • tr命令,用于转换或删除文件中的字符

    Usage: tr [OPTION]… SET1 [SET2]

    • 常用选项:
      • -c, –complement:反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
      • -d, –delete:删除指令字符
      • -s, –squeeze-repeats:缩减连续重复的字符成指定的单个字符
      • -t, –truncate-set1:削减 SET1 指定范围,使之与 SET2 设定长度相等

案例1:将/etc/passwd文件中的前5行内容转换为大写后保存至/tmp/passwd.out文件中
[root@node1 ~]# head ‐n 5 /etc/passwd | tr 'a‐z' 'A‐Z' > /tmp/passwd.output

案例2:将登陆至当前系统上用户信息中的最后1行的信息转换为大写后保存至/tmp/who.out文件中
[root@node1 ~]# who | tail ‐n 1 | tr 'a‐z' 'A‐Z' >/tmp/who.out

案例3:将文件中的:删除之后重定向到新文件中
[root@test tmp]# cat output_error | tr -d ":" > output_error_new