重定向

每打开文件都会有一个文件描述符fd

  • 输入输出
    • 1 代表标准正确输出
    • 2 代表标准错误输出
      1. [root@localhost ~]# echo hello world
      2. hello world
      3. [root@localhost ~]# echo $?
      4. 0
      5. [root@localhost ~]# hahaha
      6. -bash: hahaha: 未找到命令
      7. [root@localhost ~]# echo $?
      8. 127
      输入一条正确的命令,然后通过echo $?查看上一条命令的输出码,为0则正确输出,不为0错误输出。

重定向的定义:把一条命令的输出结果不是打印到屏幕,而是打印到一个文件里,这就是一个重定向。

输出重定向

  • 输出重定向
    • :覆盖重定向

    • :追加重定向

    • 2>:覆盖重定向错误输出数据流
    • 2>>:追加重定向错误输出数据流 ```bash [root@localhost ~]# echo hello > right.file [root@localhost ~]# cat right.file hello [root@localhost ~]# echo world > right.file [root@localhost ~]# cat right.file world

[root@localhost ~]# echo hello >> right.file [root@localhost ~]# cat right.file world hello

[root@localhost ~]# hahaha > right.file 2> wrong.file [root@localhost ~]# cat right.file [root@localhost ~]# cat wrong.file -bash: hahaha: 未找到命令

[root@localhost ~]# 123123 >> right.file 2>> wrong.file [root@localhost ~]# cat right.file [root@localhost ~]# cat wrong.file -bash: hahaha: 未找到命令 -bash: 123123: 未找到命令

  1. 标准正确输出和错误输出到不同文件:<br />`command > right.file 2> wrong.file /dev/null:垃圾桶`
  2. - 合并标准输出和错误输出为同一个数据流进行重定向
  3. - &>:覆盖重定向
  4. - &&>:追加重定向
  5. - 2>&1:将错误流重定向到标准输出文件中
  6. - 1>&2:将正确流重定向到错误输出文件中
  7. ```bash
  8. [root@localhost ~]# echo hello > all.file
  9. [root@localhost ~]# hahaha &>> all.file
  10. [root@localhost ~]# cat all.file
  11. hello
  12. -bash: hahaha: 未找到命令
  13. [root@localhost ~]# echo hello 2>> wrong.file 1>&2
  14. [root@localhost ~]# cat wrong.file
  15. -bash: hahaha: 未找到命令
  16. -bash: 123123: 未找到命令
  17. hello

输入重定向

<<EOF 指定结束符

  1. 覆盖:
  2. cat > /path/to/somefile <<EOF
  3. ....
  4. EOF
  5. 追加:
  6. cat >> /path/to/somefile <<EOF
  7. ....
  8. EOF

![$K%SFSFN{UU451K_Y7$`}6.png

管道

command1 | command2 | command3
前一个命令的输出结果会作为后一个命令的参数
Note:最后一个命令会在当前shell进程的子shell进程中执行

小工具

tr命令

tr [option] [set1] [set2]
常用选项:
-d:删除
案例1:
将/etc/passwd文件中的前5行内容转换为大写后保存至/tmp/passwd.out文件中

  1. [root@node1 ~]# head -n 5 /etc/passwd | tr 'a-z' 'A-Z' > /tmp/passwd.output

案例2:
将登陆至当前系统上用户信息中的最后1行的信息转换为大写后保存至/tmp/who.out文件中

  1. [root@node1 ~]# who | tail -n 1 | tr 'a-z' 'A-Z' >/tmp/who.out

wc命令

统计行数、单词数和字符数。
常用选项:

  • -l:行数
  • -w:单词数
  • -c:字符数

vim test1
]6@XEF{S0(7W4R0H~$S787R.png

  1. [root@localhost ~]# wc test1
  2. 4 16 96 test1
  3. [root@localhost ~]# wc -l test1
  4. 4 test1
  5. [root@localhost ~]# wc -w test1
  6. 16 test1
  7. [root@localhost ~]# wc -c test1
  8. 96 test1

cut 命令

分割
常用选项

  • -d:指定分隔符
  • -f:指定第几列

    1. [root@localhost ~]# cut -d" " -f1,4 test1
    2. hello china
    3. hello china
    4. hello china
    5. hello china
    6. [root@localhost ~]# cut -d" " -f1-3 test1
    7. hello world hello
    8. hello world hello
    9. hello world hello
    10. hello world hello

    指定分隔符为“ ”空格,取test1中的第一列和第四列,再取第一列到第三列

    sort 命令

    排序(默认为升序)
    常用选项

  • -f:忽略大小写

  • -r:逆序
  • -t:字段分隔符
  • -k #:以指定字段为标准排序
  • -n:以数值进行排序
  • -u:排序后去重
    1. [root@localhost ~]# vim test2
    2. [root@localhost ~]# cat test2
    3. 1
    4. 2
    5. 3
    6. 4
    7. 5
    8. 6
    9. 7
    10. 7
    11. 8
    12. 6
    13. 5
    14. 4
    15. 3
    16. 2
    17. 2
    18. 1
    19. [root@localhost ~]# sort test2
    20. 1
    21. 1
    22. 2
    23. 2
    24. 2
    25. 3
    26. 3
    27. 4
    28. 4
    29. 5
    30. 5
    31. 6
    32. 6
    33. 7
    34. 7
    35. 8
    36. [root@localhost ~]# sort -r test2
    37. 8
    38. 7
    39. 7
    40. 6
    41. 6
    42. 5
    43. 5
    44. 4
    45. 4
    46. 3
    47. 3
    48. 2
    49. 2
    50. 2
    51. 1
    52. 1
    53. [root@localhost ~]# sort -u test2
    54. 1
    55. 2
    56. 3
    57. 4
    58. 5
    59. 6
    60. 7
    61. 8

    uniq工具

    删除重复的项

文件testfile中第 2、3、5、6、7、9行为相同的行,使用 uniq 命令删除重复的行,可使用以下命令:
uniq testfile

常用选项:
-c:显示每行重复出现的次数
-d:仅显示重复过的行
-u:仅显示不曾重复的行

案例1:
以:为分割,取出/etc/passwd文件的第6列至第10列,并将这些信息按照第3个字段的数值大小进行排序,最
后仅显示一个字段

[root@localhost ~]# cut -d: -f6-10 /etc/passwd |cut -f3 | sort -n |uniq -c
      1 /bin:/sbin/nologin
      1 /root:/bin/bash
      1 /root:/sbin/nologin
      1 /sbin:/bin/sync
      4 /:/sbin/nologin
      1 /sbin:/sbin/halt
      1 /sbin:/sbin/nologin
      1 /sbin:/sbin/shutdown
      1 /usr/games:/sbin/nologin
      1 /var/adm:/sbin/nologin
      1 /var/empty/sshd:/sbin/nologin
      1 /var/ftp:/sbin/nologin
      1 /var/lib/chrony:/sbin/nologin
      1 /var/spool/lpd:/sbin/nologin
      1 /var/spool/mail:/sbin/nologin
      1 /var/spool/postfix:/sbin/nologin