views:

86

answers:

3

Imaginary Situation: You’ve used mysqldump to create a backup of a mysql database. This database has columns that are blobs. That means your “text” dump files contains both strings and binary data (binary data stored as strings?)

If you cat this file to the screen

$ cat dump.mysql

you’ll often get unexpected results. The terminal will start beeping, and then the output finishes scrolling by you’ll often have garbage chacters entered on your terminal as through you’d typed them, and sometimes your prompts and anything you type will be garbage characters.

Why does this happen? Put another way, I think I’m looking for an overview of what’s actually happening when you store binary strings into a file, and when you cat those files, and when the results of the cat are reported to the terminal, and any other steps I’m missing.

+4  A: 

Start here: http://www.faqs.org/docs/Linux-HOWTO/Keyboard-and-Console-HOWTO.html

In particular, sections 3 (Console generalities) and section 4 (reseting your terminal).

It covers a bit more than you're talking about, but should give you what you need.

Daniel Martin
More information is always better than less!
Alan Storm
+6  A: 

When you cat a binary file you can inadvertently send control characters to the terminal.

If a terminal application wants to send a beep for example, it sends the following binary to the terminal: 0x007 (SYS V only).

The same goes for colors, cursor position and others.

Byron Whitlock
When you say, "the terminal application sends the following binary to the terminal ..." what's the difference between the terminal and the terminal application?
Alan Storm
+2  A: 

When you cat the binary data to the screen, the terminal tries to interpret that binary data into ASCII (or UTF). Some characters are capable of controlling the terminal. For example,

echo "^[[0;31;40m" # The first ^[ comes from pressing Ctrl+v, Esc

Will turn the background black and the foreground red. Use reset to return your terminal to normal.

amertune