cat and more有不同的输出

时间:2018-01-12 12:09:57

标签: linux bash shell unix scripting

当我尝试执行以下操作时,两个输出都不同:

$ cat /var/log/log

BACKUP-2017_12_30_03-35-02.ta 100% 5330MB  14.2MB/s   06:16

BACKUP-2018_01_09_03-35-02.ta 100% 5342MB  14.7MB/s   06:03

BACKUP-2018_01_02_03-35-02.ta 100% 5312MB  14.6MB/s   06:03

BACKUP-2018_01_06_03-35-02.ta 100% 5328MB  14.4MB/s   06:11

more /var/log/log具有与上面相同的输出,但是从1%到100%都有。

我必须在下面的脚本中使用cat的输出,但是当我这样做时,输出将是更多的输出。

echo "Below are the files copied during RUN" && echo -e "\n----- Contents -----\n" && cat /var/log/log) | tr -dc '[[:print:]]' | mailx -v \

1 个答案:

答案 0 :(得分:0)

控制台上使用了一些特殊字符和序列,这些特殊字符用于各种目的,例如移动光标位置,还可以产生声音和设置各种终端选项。这些被称为转义序列,例如,您可以在此处阅读https://en.wikipedia.org/wiki/ANSI_escape_code 在(普通)控制台终端上执行cat <file>将/应解析大多数/某些转义序列。您可能会看到保存到这个命令的文件的命令输出将在终端上运行(粉扑,我不知道如何解释它,请参阅下面的示例)。 more解析转义序列有点智能,因此输出更加用户友好 让我们在一个例子中展示它。想象一下,您正在使用wget:

下载文件
$ wget http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
--2018-01-12 14:10:37--  http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.5’

        install_sw4stm32_linux_   0%[                                                      ]   2.91M  1.42MB/s             

最后一行'更改/刷新自己',我的意思是,wget生成转义序列以将光标位置移动到行的开头(可能是'\ r')并再次打印该行,从而刷新它。现在我们将欺骗wget认为它的输出是控制台并将它的stderr和stdout输出保存到文件/ tmp / 1.

 unbuffer wget http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run >&/tmp/1

现在让我们:

$ cat /tmp/1
--2018-01-12 14:10:27--  http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.4’

     install_sw4stm32_linux_64b   1%[                                                      ]   5.87M  2.63MB/s     

然而,这并不完全是文件的内容。例如,在vim中你可能会看到:

$ vim /tmp/1
--2018-01-12 14:10:27--  http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.4’

^M                install_sw4stm3   0%[                                                      ]       0  --.-KB/s               ^M               install_sw4stm32   0%[                                                      ] 596.40K  2.91MB/s               ^M              install_sw4stm32_   0%[                                                      ]   1.66M  4.14MB/s               ^M             install_sw4stm32_l   0%[                                                      ]   2.41M  4.01MB/s               ^M            install_sw4stm32_li   0%[                                                      ]   3.11M  3.87MB/s               ^M           install_sw4stm32_lin   0%[                                                      ]   3.60M  3.57MB/s               ^M          install_sw4stm32_linu   0%[                                                      ]   3.94M  3.23MB/s               ^M         install_sw4stm32_linux   1%[                                                      ]   4.30M  3.02MB/s               ^M        install_sw4stm32_linux_   1%[                                                      ]   4.69M  2.88MB/s               ^M       install_sw4stm32_linux_6   1%[                                                      ]   5.08M  2.78MB/s               ^M      install_sw4stm32_linux_64   1%[                                                      ]   5.48M  2.70MB/s               ^M     install_sw4stm32_linux_64b   1%[                                                      ]   5.87M  2.63MB/s  

输出中的那些^M符号就像vim告诉你的那样,那里有一个特殊字符(不是字符^M,而是一个特殊的转义序列/字符) 。在这种情况下,它是用于将光标移动到文件开头的转义序列。 更多在解析转义序列时有点聪明:

$ more /tmp/1
--2018-01-12 14:10:27--  http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.4’

             install_sw4stm32_l   0%[                                                      ]   2.41M  4.01MB/s               
         install_sw4stm32_linux   1%[                                                      ]   4.30M  3.02MB/s               
     install_sw4stm32_linux_64b   1%[                                                      ]   5.87M  2.63MB/s               

希望能澄清你与猫的不一致和更多的输出。