The magic of MySQL client output formatting

Source: Internet
Author: User

The output formatting of the MySQL client is amazing. I didn't notice it before. I 'd like to share it here:
$ Mysql-uroot-e "show global status like 'uptime '"
+ ----- + --- +
| Variable_name | Value |
+ ----- + --- +
| Uptime | 2174838 |
+ ----- + --- +

$ Mysql-uroot-e "show global status like 'uptime'">/tmp/test
[Mysql @ dev81 ~] $ Cat/tmp/test
Variable_name Value
Upload 2174855

Have you found anything strange? The formatted symbols are gone! With the same command, after redirecting to a file, the output changes!
+ ----- + --- +
| Variable_name | Value |
+ ----- + --- +
| Uptime | 2174838 |
+ ----- + --- +
Changed
Variable_name Value
Upload 2174855

I thought for a long time and didn't want to understand it. When asked about the lower part of the qushan city, he started to use strace without saying anything, and immediately found the secret.
$ Strace mysql-uroot-e "show global status like 'uptime '"
......
......
Write (1, "+ ----- + --- + \ n", 28 + ----- + --- +
) = 28
Write (1, "| Variable_name | Value | \ n", 28 | Variable_name | Value |
) = 28
Write (1, "+ ----- + --- + \ n", 28 + ----- + --- +
) = 28
Write (1, "| Uptime | 2175430 | \ n", 29 | Uptime | 2175430 |
) = 29
Write (1, "+ ----- + --- + \ n", 28 + ----- + --- +
) = 28
Poll ([{fd = 3, events = POLLIN | POLLPRI}], 1, 0) = 0
Write (3, "\ 1 \ 0 \ 0 \ 1", 5) = 5
Shutdown (3, 2/* send and receive */) = 0
Close (3) = 0
Exit_group (0) =?
Process 24577 detached

$ Strace mysql-uroot-e "show global status like 'uptime'">/tmp/test
......
......
Write (1, "Variable_name \ tValue \ nUptime \ t21755 ″..., 35) = 35
Exit_group (0) =?
Process 24581 detached

For different output channels, the MySQL client seems to have a way to capture and adopt different formatting output policies. However, I still don't know the reason why MySQL is doing this. It is estimated that only days will know.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.